我使用json2csharp从我的JSON生成一些类。当我使用DataContractJsonSerializer
或JsonConvert
进行反序列化时,一切正常,但我无法理解为什么生成器会生成Message2
类。它不符合我的逻辑,我想清楚地理解这一点。我认为Message2
应该在OfflineMessage
内部List<Message2> messages
使用,Message1
类需要删除。我错过了什么?
这些是由json2csharp生成的类(请注意,我将Message
重命名为Message1
以获得更清晰的问题):
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; }
}
这就是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=="
}
}
]
}
}
]
}
答案 0 :(得分:1)
它正在创建一个Message2
类,因为您的messages
属性是一个对象数组,而这些对象包含其message
属性中的其他对象。它需要一个类来表示messages
数组的成员,而另一个类来表示message
属性的值。
如果您负责JSON格式,那么构建JSON可能会更清晰:
{
"@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=="
}
]
}
}
]
}
这应该产生你期望的那种类结构。