如何在C#中反序列化复杂的Dictionary list JSON?

时间:2016-12-01 14:21:17

标签: c# json dictionary

我有以下JSON。它可以使用以下代码反序列化。

List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>> listOfOptions = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>>(JSONdata);

我想在类结构之后构建listOfOptions


fieldName =&#34; prioritycode&#34;

fieldOption 1 =&gt;键=高,值= 1

fieldOption 2 =&gt;键=正常,值= 2

fieldOption 3 =&gt;键=低,值= 3

isescalatedfirstresponsesent的相同方式。

internal class DropOptions
{
    public string fieldName { get; set; }
    public Dictionary<string,int> fieldOptions { get; set; }

}

我已经尝试了足够但不能构建:(。

有人可以帮助我吗?

这是我的JSON

  

[{&#34; prioritycode&#34; {&#34;高&#34; {&#34;高&#34;:1},&#34;正常&#34; {&#34 ;正常&#34;:2},&#34;低&#34; {&#34;低&#34;:3}}},{&#34; isescalated&#34; {&#34;是&# 34;:{&#34;是&#34;:1},&#34;否&#34; {&#34;否&#34;:0}}},{&#34; firstresponsesent&#34 ;: {&#34;是&#34; {&#34;是&#34;:1},&#34;否&#34; {&#34;否&#34;:0}}}]

[
    {
        "prioritycode":
        {
            "High":
            {
                "High":1
            },
            "Normal":
            {
                "Normal":2
            },
            "Low":
            {
                "Low":3
            }
        }
    },
    {
        "isescalated":
        {
            "Yes":
            {
                "Yes":1
            },
            "No":
            {
                "No":0
            }
        }
    },
    {
        "firstresponsesent":
        {
            "Yes":
            {
                "Yes":1
            },
            "No":
            {
                "No":0
            }
        }
    }
]

3 个答案:

答案 0 :(得分:0)

您可以使用 Json.Net ,只需编写类似var obj = JsonConvert.DeserializeObject<dynamic>(json);的内容 在反序列化json后,您已经提供了我能够写int normal = obj[0].prioritycode.Normal.Normal;并且normal变量持有2。不需要额外的课程。

答案 1 :(得分:0)

I have done by this. It looks fine. I know it is bit difficult and having loops. But the options(prioritycode,isescalated,firstresponsesent) are dynamic. It can be more or less. Name of it can be anything else like taskStatus, productType, productCategory etc.

List<DropOptions> listOfOptions = new List<DropOptions>();
foreach (Dictionary<string, Dictionary<string, Dictionary<string, int>>> item in tempListOfOptions)
{
    foreach (Dictionary<string, Dictionary<string, int>> item1 in item.Values)
    {
        DropOptions objDrop = new DropOptions();
        objDrop.fieldName = item.Select(t => t.Key).FirstOrDefault();
        objDrop.fieldOptions = new Dictionary<string, int>();
        foreach (Dictionary<string, int> item2 in item1.Values)
        {
            string strKey = item2.Select(t => t.Key).FirstOrDefault();
            int strValue = item2.Select(t => t.Value).FirstOrDefault();

            objDrop.fieldOptions.Add(strKey, strValue);
        }
        listOfOptions.Add(objDrop);
    }
}

答案 2 :(得分:-1)

这使用Newtonsoft.Json(Json.NET)框架将JSON反序列化为RootObject的集合。使用强类型类的好处是您有编译时检查,并且您没有尝试访问不存在的属性或拼写错误的属性。

public class RootObjectDeserializer
{
    List<RootObject> DeserializeRoot()
    {
        // Read string from somewhere
        string json = "[{\"prioritycode\":{\"High\":{\"High\":1},\"Normal\":{\"Normal\":2},\"Low\":{\"Low\":3}}},{\"isescalated\":{\"Yes\":{\"Yes\":1},\"No\":{\"No\":0}}},{\"firstresponsesent\":{\"Yes\":{\"Yes\":1},\"No\":{\"No\":0}}}]";
        // Deserialize to a List of RootObject
        List<RootObject> deserializedObjects = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(json);
        // Return RootObject
        return deserializedObjects;
    }
}

//Class mappings below
public class High
{
    [JsonProperty("High")]
    public int HighProperty { get; set; }
}

public class Normal
{
    [JsonProperty("Normal")]
    public int NormalProperty { get; set; }
}

public class Low
{
    [JsonProperty("Low")]
    public int LowProperty { get; set; }
}

public class Prioritycode
{
    public High High { get; set; }
    public Normal Normal { get; set; }
    public Low Low { get; set; }
}

public class Yes
{
    [JsonProperty("Yes")]
    public int YesProperty { get; set; }
}

public class No
{
    [JsonProperty("No")]
    public int NoProperty { get; set; }
}

public class Isescalated
{
    public Yes Yes { get; set; }
    public No No { get; set; }
}

public class Yes2
{
    public int Yes { get; set; }
}

public class No2
{
    public int No { get; set; }
}

public class Firstresponsesent
{
    public Yes2 Yes { get; set; }
    public No2 No { get; set; }
}

public class RootObject
{
    public Prioritycode prioritycode { get; set; }
    public Isescalated isescalated { get; set; }
    public Firstresponsesent firstresponsesent { get; set; }
}