为什么json2sharp会从我的JSON中生成一个额外的类?

时间:2016-06-09 09:58:49

标签: c# json json.net deserialization datacontractserializer

我使用json2csharp从我的JSON生成一些类。当我使用DataContractJsonSerializerJsonConvert进行反序列化时,一切正常,但我无法理解为什么生成器会生成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=="
            }
          }
        ]
      }
    }
  ]
}

1 个答案:

答案 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=="
               }
            ]
         }
      }
   ]
}

这应该产生你期望的那种类结构。