简体   繁体   English

如何将JSON文档反序列化为Dictionary <string, List<object> &gt;

[英]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.

相关问题 如何将 json 列表字符串反序列化为对象字符串列表? - How do I deserialize a json list string to a list of object strings? 我如何将Json反序列化为Dictionary <String,List<String> &gt; - How I can deserialize Json into Dictionary<String,List<String>> 我如何反序列化这本词典<string,string[]> - How do i deserialize this dictionary<string,string[]> 将JSON反序列化为Dictionary <string,Object> - Deserialize JSON into Dictionary<string,Object> 如何将此JSON字符串反序列化回列表 <T> ? - How do I deserialize this JSON string back to a List<T>? 我如何反序列化json字符串列表 <Dictionary<string,int> &gt;使用JQuery? - How can I deserialize a json string List<Dictionary<string,int>> using JQuery? 将 JSON 反序列化为字典<string, List<string> &gt; - Deserialize JSON to Dictionary<string, List<string>> 将 JSON 字符串反序列化为字典<string,object> - Deserialize JSON string to Dictionary<string,object> 如何将特定的Json节点反序列化为Dictionary <string,object> ? - How to deserialize specific Json node to Dictionary<string,object>? 如何将JSON反序列化为简单的Dictionary <string,object> 使用动态的datagridview? - How can I deserialize JSON to a simple Dictionary<string,object> to datagridview using only dynamics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM