Ден Денис Ден Денис - 5 months ago 55
JSON Question

Json transformation to object by json2csharp and my understanding of this?

My problem is that all is work with DataContractJsonSerializer or JsonConvert but I am cannot understand why generator make class Message2.
It does not fit in my logic and i want to clearly understand this. I think that Message2 need use inside OfflineMessage like "List messages" and class Message1 need to clean.
In code i am rename Message to Message1 for a clearer question.
Why i am don't right ?

This is classes generated by json2csharp:

public class Message2
{
public int status { get; set; }
public string sender { get; set; }
public string receiver { get; set; }
public string msg { get; set; }
public int timeStamp { get; set; }
public string hash { get; set; }
public string msgContext { get; set; }
}

public class Message1
{
public Message2 message { get; set; }
}

public class OfflineMessage
{
public int sequence { get; set; }
public List<Message1> messages { get; set; }
}

public class Respons
{
public OfflineMessage offlineMessage { get; set; }
}

public class RootObject
{
public int __invalid_name__@pendingMsg { get; set; }
public int __invalid_name__@syncStatus { get; set; }
public List<Respons> responses { get; set; }
}


This is how look json:

{ "@pendingMsg" : 0,
"@syncStatus" : 0,
"responses" :[ { "offlineMessage" :{ "sequence" : 2,
"messages" : [{ "message" : { "status" : 6,
"sender" : "storng" ,
"receiver" : "JoilSp" ,
"msg" : "Hello my friend" ,
"timeStamp" : 1465398075,
"hash" : "+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==" ,
"msgContext" : "+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
}
}
, { "message" : { "status" : 6,
"sender" : "JioS" ,
"receiver" : "JoilSp" ,
"msg" : "nice)))" ,
"timeStamp" : 1465398075,
"hash" : "+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==" ,
"msgContext" : "+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
}
}
]
}
}
]
}

Answer

It's creating a Message2 class because your messages property is an array of objects, and those objects contain other objects in their message property. It needs one class to represent the members of the messages array, and another to represent the values of the message properties.

If you are in charge of your JSON format, it may be cleaner to structure your JSON like this:

{
   "@pendingMsg":0,
   "@syncStatus":0,
   "responses":[
      {
         "offlineMessage":{
            "sequence":2,
            "messages":[
               {
                  "status":6,
                  "sender":"storng",
                  "receiver":"JoilSp",
                  "msg":"Hello my friend",
                  "timeStamp":1465398075,
                  "hash":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==",
                  "msgContext":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
               },
               {
                  "status":6,
                  "sender":"JioS",
                  "receiver":"JoilSp",
                  "msg":"nice)))",
                  "timeStamp":1465398075,
                  "hash":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==",
                  "msgContext":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
               }
            ]
         }
      }
   ]
}

This should produce the kind of class structure that you are expecting.

Comments