简体   繁体   中英

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.

How do I do that when my JSON document looks like this?

{
   "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:

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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