Asp.net JSON Deserialize problem

Posted by Billy on Stack Overflow See other posts from Stack Overflow or by Billy
Published on 2010-05-11T05:49:28Z Indexed on 2010/05/11 5:54 UTC
Read the original article Hit count: 333

Filed under:
|

I want to deserialize the following JSON string:

[
{"name":"photos","fql_result_set":[{"owner":"123456","caption":"Caption 1", "object_id":123},{"owner":"223456","caption":"Caption 2", "object_id":456}]},

{"name":"likes","fql_result_set":[{"object_id":123,"user_id":12156144},{"object_id":456,"user_id":140342725}]}
]

and get the POCO like

[DataContract]
public class Photo{

    [DataMember]
    public string owner{get;set;}

    [DataMember]
    public string caption{get;set;}

    [DataMember]
    public string object_id{get;set;}

}

[DataContract]
public class Like
{
    [DataMember]
    public string object_id { get; set; }

    [DataMember]
    public string user_id { get; set; }
}

What should I do?

I already have this piece of code:

public class JSONUtil
{
    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
        obj = (T)serializer.ReadObject(ms);
        ms.Close();
        return obj;
    }

© Stack Overflow or respective owner

Related posts about JSON

Related posts about ASP.NET