简体   繁体   English

如何使用嵌套列表反序列化Json

[英]How to deserialize Json with nested lists

It's not my first time deserializing Json into c# classes but I am having some trouble when these Json have nested lists. 这不是我第一次将Json反序列化为c#类,但是当这些Json有嵌套列表时我遇到了一些麻烦。 Here is the returned Json of a list of workOrders: 这是workOrders列表的返回Json:

[
  {
    "_id": "5cacdda0b4e6d128a61187ca",
    "taskInstance": [
      {
        "_id": "5cacdda0b4e6d128a61187cc"
      }
    ],
    "isRunning": false
  },
  {
    "_id": "5cacebb0b4e6d128a61187d1",
    "taskInstance": [
      {
        "_id": "5cacebb0b4e6d128a61187d3"
      }
    ],
    "isRunning": false
  }
]

and here the C# classes: WorkOrder 这里是C#类:WorkOrder

public class WorkOrder{
    public string Id { get; set; }
    public List<TaskInstance> TaskInstances { get; set; }
    public string IsRunning { get; set; }
}

and TaskInstance 和TaskInstance

public class TaskInstance{
    public string Id { get; set; }
}

And here is the code that I'm using to check the result: 这是我用来检查结果的代码:

JObject jsonResponse = JObject.Parse(data["data"].ToString());
string workOrderJson = jsonResponse["workOrderInstances"].ToString();
List<WorkOrder> wos = JsonConvert.DeserializeObject<List<WorkOrder>>(workOrderJson);

foreach (WorkOrder wo in wos){
    Debug.Log("WO _id: " + wo.Id);
    Debug.Log("wo.TaskInstances: "+ wo.TaskInstances);
    Debug.Log("WO isRunning: " + wo.IsRunning);
}

And while I'm seeing the correct values for WO._id and WO.isRunning wo.TaskInstances seems to be returning null or at leat throws a null reference exception whenever I try to access it. 虽然我看到了WO._id和WO.isRunning wo.TaskInstances的正确值,但是当我尝试访问它时,似乎返回null或者leat会抛出一个空引用异常。

I've searched other topics before but none of the solutions seem to work. 我之前搜索过其他主题,但没有一个解决方案似乎有效。 I'm not sure what I am missing. 我不确定我错过了什么。 Thank you for your help. 谢谢您的帮助。

Disclaimer: the raw data in JObject.Parse(data["data"] looks like this: 免责声明: JObject.Parse中的原始数据 (data [“data”]如下所示:

{
  "data": {
    "workOrderInstances": [
      {
        "_id": "5cacdda0b4e6d128a61187ca",
        "taskInstance": [
          {
            "_id": "5cacdda0b4e6d128a61187cc"
          }
        ],
        "isRunning": false
      },
      ...
     ]
  }
}

Use this model: 使用这个模型:

Because the problem with your model is: JSON property name and model property name is not the same in your code. 因为模型的问题是:JSON属性名称和模型属性名称在您的代码中是不同的。

So whenever you need your own custom name in property use 因此,无论何时您需要在属性中使用自己的自定义名称
[JsonProperty("JsonPropertyName")]

 public partial class Root
    {
        [JsonProperty("_id")]
        public string Id { get; set; }

        [JsonProperty("taskInstance")]
        public TaskInstance[] TaskInstances { get; set; }

        [JsonProperty("isRunning")]
        public bool IsRunning { get; set; }
    }

    public partial class TaskInstance
    {
        [JsonProperty("_id")]
        public string Id { get; set; }
    }

Let me know if it works for you. 请让我知道这对你有没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM