Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

JsonString to .Net Object conversion returning null always

$
0
0

I tried to convert a json string in specific format to a .Net object , but the object always shows null.Any idea why it is not parsing json String?

The jsonString i tried to parse is

[{"id":"4408","form":"","name":"og","explanation":"","gender":"","node_class":"Phrase","word_class":{"id":"27","name":"konjunksjon","node_class":"WordClass"},"conjugations":[]}]

beautified jsonstring of above is

[{
    "id": "4408",
    "form": "",
    "name": "og",
    "explanation": "",
    "gender": "",
    "node_class": "Phrase",
    "word_class": {
        "id": "27",
        "name": "konjunksjon",
        "node_class": "WordClass"
    },
    "conjugations": []
}]

Code i used for conversion is

            string response ="[{\"id\":\"4408\",\"form\":\"\",\"name\":\"og\",\"explanation\":\"\",\"gender\":\"\",\"node_class\":\"Phrase\",\"word_class\":{\"id\":\"27\",\"name\":\"konjunksjon\",\"node_class\":\"WordClass\"},\"conjugations\":[]}]";
            Response respose = new Response();
            try
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(respose.GetType());
                System.IO.MemoryStream ms = new System.IO.MemoryStream(Encoding.Unicode.GetBytes(response));
                respose = serializer.ReadObject(ms) as Response;
            }
            catch
            {
            }

And structre of .Net classes i used are

Serializable, DataContract(Name = "response")]
    public class Response
    {
        //form  name explanation gender node_class word_class conjugations
        [DataMember(Name = "id")]
        public string Id { get; set; }

        [DataMember(Name = "form")]
        public string Form { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "explanation")]
        public string Explanation { get; set; }

        [DataMember(Name = "gender")]
        public string Gender { get; set; }

        [DataMember(Name = "node_class")]
        public string NodeClass { get; set; }
        [DataMember(Name="word_class")] 
        public WordClassModel WordClass{ get; set; } 
        [DataMember(Name = "conjugations")] 
        public List<string> Conjugations { get; set; } 

    }
    [Serializable, DataContract(Name = "word_class")]
    public   class WordClassModel
    {
        [DataMember(Name = "id")]
        public string Id { get; set; }
        [DataMember(Name = "name")]
        public int Name { get; set; }
        [DataMember(Name = "node_class")]
        public string NodeClass { get; set; }
    }

Can someone find out reason why the conversion returning null always?


Thanks ***Share Knowledge to gain more***


Viewing all articles
Browse latest Browse all 18858