0
votes

I am new to wp7 programming and i would like to know how can i store all contacts which include first name, last name, email and phone number in json fomat from a wp7. By now i am able to read these things and store in isolated storage file but i need them as Json.

Thanks in advance, any help would be appreciated.

hi this is my code but now i am facing problem to get values from data memebers of class mycontacts such that it can be passed to json serializer. Can you please help me in this. I want to store the values in object such that it can be converted to using the way you gave.

private void ButtonContacts_Clk(object sender, RoutedEventArgs e) { Contacts cContacts = new Contacts(); cContacts.SearchCompleted += new EventHandler(ContactsSearch); cContacts.SearchAsync(String.Empty, FilterKind.DisplayName, null); }

    void ContactsSearch(object sender, ContactsSearchEventArgs e)
    {
        for (int i = 0; i < e.Results.Count(); i++)
        {
            var ContactsData = from m in e.Results
                      select new mycontacts
                      {
                          DisplayName = m.DisplayName,
                          PhoneNumber = m.PhoneNumbers.FirstOrDefault()  
                       };
        }
    }

}

public class mycontacts
{
   public String fname { get; set; }
   public String lname { get; set; }
   public String DisplayName { get; set; }
   public String[] email { get; set; }
   public Phone[] phone_num;
}
public class Phone
{
    public String number { get; set; }
    public int type { get; set; }
}
1
Hi, what do you mean by 'I am facing problem to get values from data memebers of class mycontacts'? What problem are you facing? - Amulya Khare

1 Answers

1
votes

Here is a simple code snippet to convert an object to JSON string in windows phone:

Contact c = new Contact("FirstName", "LastName", "[email protected]", "9808928");
/// Serialize to JSON
DataContractJsonSerializer serializer = new DataContractJsonSerializer(c.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, c);
string json = Encoding.Default.GetString(ms.ToArray());

Hope this helps!