none
JsonString to .Net Object conversion returning null always RRS feed

  • Question

  • 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 string 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***


    • Moved by Mike DanesModerator Sunday, June 16, 2013 12:32 PM not related to WPF
    • Edited by FullPgmr Monday, June 17, 2013 2:31 AM a small edit in code
    Saturday, June 15, 2013 6:42 AM

Answers

  • Yes.  Now that you have changed the property type, it is as simple as changing your inbound string to not include the outer braces:

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

    Let me know if this works for you.

    Cheers,

    Jeff


    Jeff

    Monday, June 17, 2013 2:52 AM
  • found  the solution as well , i tried to parse json to a List and it works for me with '[' 

     respose = serializer.ReadObject(ms) as List<Response>;

    Thank you so much for  pointing me to find the problem with conversion


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


    Monday, June 17, 2013 3:57 AM

All replies

  • Hello,

    It might be as simple as the WorldClassModel.Name is marked as an int but you intend it to be a string.  In the example you have \"name\":\"konjunksjon\".

    Give this a try and if it is not the root cause, let me know and I can look some more.

    Cheers,

    Jeff


    Jeff

    Sunday, June 16, 2013 11:35 PM
  • I tried with changing the property type to string , but still i am getting null values for .net object.

    Is there anything special with the json string ? any idea ?


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

    Monday, June 17, 2013 2:31 AM
  • Yes.  Now that you have changed the property type, it is as simple as changing your inbound string to not include the outer braces:

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

    Let me know if this works for you.

    Cheers,

    Jeff


    Jeff

    Monday, June 17, 2013 2:52 AM
  • Yes its working now .

    But how can i handle a json string begins with '[' , Since the string sample i posted in question was the  response of a server API method, and its   returns me the json string enclosed inside '['  ?

    Is it possible to do so ? or do i need to manually remove the '[' instance before conversion?


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

    Monday, June 17, 2013 3:44 AM
  • found  the solution as well , i tried to parse json to a List and it works for me with '[' 

     respose = serializer.ReadObject(ms) as List<Response>;

    Thank you so much for  pointing me to find the problem with conversion


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


    Monday, June 17, 2013 3:57 AM