C# 如何在C中反序列化复杂的字典列表JSON?

C# 如何在C中反序列化复杂的字典列表JSON?,c#,json,dictionary,C#,Json,Dictionary,我有以下的JSON。可以使用以下代码对其进行反序列化 List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>> listOfOptions = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, Dictionary<string, int>

我有以下的JSON。可以使用以下代码对其进行反序列化

List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>> listOfOptions = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>>(JSONdata);
我已经尝试了足够多,但无法构建:

谁能帮帮我吗

这是我的JSON

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

您可以使用Json.Net,只需编写var obj=JsonConvert.DeserializeObjectjson;
在反序列化您提供的json之后,我能够编写int normal=obj[0].prioritycode.normal.normal;正态变量为2。不需要额外的类。

这使用Newtonsoft.Json.NET框架将Json反序列化为一个根对象集合。使用强类型类的好处是,您可以进行编译时检查,并且确信您不会试图访问不存在或拼写错误的属性

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

我已经这样做了。看起来不错。我知道这是有点困难,有循环。但是选项PriorityCode是可伸缩的,firstresponsesent是动态的。它可以是多多少少。它的名称可以是任何其他名称,如taskStatus、productType、productCategory等

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

老实说,只需使用类映射。你能给我一个主意吗?不要用一堆字典来保存字典的钥匙@json2csharp.com?我已经为此浪费了足够的时间。如果字段名已修复,则您的方法将起作用。您已修复字段的名称。就我而言,它可以是任何东西。我们不能依赖fix fieldsprioritycode、isescalated、firstresponsesent或fix选项High、Normal、Low。为什么您要投票否决有效答案,因为它们不符合您未提及的要求中的特定标准?这很简单,只是不要把它们作为答案。
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; }
}
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);
    }
}