[英]How do I deserialize a JSON document into a Dictionary<string, List<object>>
I'm trying to deserialize a JSON document into a class with only using the Json*
tags on my class. 我试图仅通过在类上使用Json*
标签将JSON文档反序列化为一个类。
How do I do that when my JSON document looks like this? 当我的JSON文档如下所示时,该怎么办?
{
"id": "1",
"name" : "test",
"dictionary": [
{
"obj" : "a",
"list" : [{ "Id" : "1"} , { "Id" : "2"}]
},
{
"obj" : "b",
"list" : [{ "Id" : "3"} , { "Id" : "4"}]
}
]
}
My C# class looks like this: 我的C#类如下所示:
public class Test
{
[JsonProperty("id")]
public string TestId {get;set;}
[JsonProperty("name")]
public string Name {get;set;}
[JsonProperty("dictionary")]
public Dictionary(string, List<object>) dict {get;set;}
}
However, it fails to deserialize into the dictionary. 但是,它无法反序列化到字典中。 My code right now is leveraging a documentDb query like this: 现在,我的代码正在利用documentDb查询,如下所示:
var response = _client.CreateDocumentQuery<Test>(collection.DocumentsLink, "SELECT * FROM root r");
You need to create POCO classes for each of the nested objects in your JSON object graph. 您需要为JSON对象图中的每个嵌套对象创建POCO类。
Try deserializing with the following classes: 尝试使用以下类进行反序列化:
public class Test
{
[JsonProperty("id")]
public string TestId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("dictionary")]
public List<TestInnerItem> dictionary { get; set; }
}
public class TestInnerItem
{
public string Obj { get; set; }
public List<TestInnerInnerItem> List { get; set; }
}
public class TestInnerInnerItem
{
public string Id { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.