簡體   English   中英

如何將JSON文檔反序列化為Dictionary <string, List<object> &gt;

[英]How do I deserialize a JSON document into a Dictionary<string, List<object>>

我試圖僅通過在類上使用Json*標簽將JSON文檔反序列化為一個類。

當我的JSON文檔如下所示時,該怎么辦?

{
   "id": "1",
   "name" : "test",
   "dictionary": [
                    {
                       "obj" : "a",
                       "list" : [{ "Id" : "1"} , { "Id" : "2"}]
                    },
                    {
                       "obj" : "b",
                       "list" : [{ "Id" : "3"} , { "Id" : "4"}]
                    }
                 ]

}

我的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;}
}

但是,它無法反序列化到字典中。 現在,我的代碼正在利用documentDb查詢,如下所示:

var response = _client.CreateDocumentQuery<Test>(collection.DocumentsLink, "SELECT * FROM root r");

您需要為JSON對象圖中的每個嵌套對象創建POCO類。

嘗試使用以下類進行反序列化:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM