Search code examples
objectserializationdatacontractarraysjsonserializer

deserializing string array in c# for wp7


This is the structure of my JSON:

string sample = 
    "[{'Disp_Name':'avi garg',
       'emailId':'avi@india.com',
       'fName':'avi',
       'lName':'garg',
       'ph':{'number':'9813612344(Mobile)','type':1}
      },
      {'Disp_Name':'monk gup',
       'emailId':'mon@india.com',
       'fName':'monk',
       'lName':'gup',
       'ph':{'number':'01127243480(home)','type':2}
      }]";

And I want to deserialize it back to an object array of my class. Can anyone please help me out in doing that? I would like to use datacontractjsonserializer preferably but others are also fine.

Thanking you


Solution

  • public static List<your class> decrypt_json(string json)
        {
            var deserializedUser = new List<your class>();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser=new DataContractJsonSerializer(deserializedUser.GetType());
            deserializedUser =  ser.ReadObject(ms) as List<your class>;
            MessageBox.Show(deserializedUser.Count().ToString());
            ms.Close();
            return deserializedUser;       
        }