0

I am unable to deserialize my Json String with the help of "DataContractJsonSerializer". I can't use third party tool for deserialization, my json string has no any enter code hereMainObject so i am unable to do, Please help me. My json data is below.

    [{
    "id": "2348",
    "fo": "",
    "na": "og",
    "ex": "",
    "ge": "",
    "no_cl": "Phr",
    "wo_cl": {
            "id": "27",
            "na": "kon",
            "na_cl": "WordClass"
            },
    "con": []
}]

my classes according to above Json is following.

    public class WoCl
    {
        public string id { get; set; }
        public string na { get; set; }
        public string na_cl { get; set; }
    }

    public class RootObject
    {
        public string id { get; set; }
        public string fo { get; set; }
        public string na { get; set; }
        public string ex { get; set; }
        public string ge { get; set; }
        public string no_cl { get; set; }
        public WoCl wo_cl { get; set; }
        public List<object> con { get; set; }
    }

My deserializing code is following.

    string json = returnDictionaryJsonFromServer();//this function returning above json data
    List<MainObject> obj = new List<MainObject>();
    var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json));
    var serializer = new DataContractJsonSerializer(obj.GetType());
    obj = serializer.ReadObject(memoryStream) as List<MainObject>;
    return obj;
4

1 回答 1

0
public AllAnnotatedData Deserializer(string json)
{
     MyWrapper obj = new MyWrapper();
     using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
     {
         try
         {
             var serializer = new DataContractJsonSerializer(obj.GetType());
             obj = (MyWrapper)serializer.ReadObject(memoryStream);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
     return obj;
}
//MyWrapper is following.

[DataContract]
public class MyWrapper
{
    [DataMember(Name = "objects")]
    public List<mySecondWrapper> mySecondWrapper //this is list of my wrapper class.
    { get; set; }

    [DataMember(Name = "property1")]
    public Meta AllAnnotationMeta
    { get; set; }

    [DataMember(Name = "property2")]
    public List<myThirdWrapper> myThirdWrapper //this is list of my wrapper class.
    { get; set; }
}
于 2013-09-05T05:26:52.347 回答