且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Json数组转换为C#中的对象列表

更新时间:2022-11-19 09:14:40

正如其他人已经指出的那样,您没有得到预期结果的原因是您的 JSON 与您尝试反序列化的类结构不匹配进入.您要么需要更改 JSON,要么更改您的类.由于其他人已经展示了如何更改 JSON,我将在此处采取相反的方法.

As others have already pointed out, the reason you are not getting the results you expect is because your JSON does not match the class structure that you are trying to deserialize into. You either need to change your JSON or change your classes. Since others have already shown how to change the JSON, I will take the opposite approach here.

为了匹配您在问题中发布的 JSON,您的类应定义如下.请注意,我进行了以下更改:

To match the JSON you posted in your question, your classes should be defined like those below. Notice I've made the following changes:

  1. 我添加了一个 Wrapper 类,对应于您的 JSON 中的外部对象.
  2. 我将 ValueSet 类的 Values 属性从 List 更改为 Dictionary; 因为 JSON 中的 values 属性包含一个对象,而不是数组.
  3. 我添加了一些额外的 [JsonProperty] 属性来匹配 JSON 对象中的属性名称.
  1. I added a Wrapper class corresponding to the outer object in your JSON.
  2. I changed the Values property of the ValueSet class from a List<Value> to a Dictionary<string, Value> since the values property in your JSON contains an object, not an array.
  3. I added some additional [JsonProperty] attributes to match the property names in your JSON objects.

类定义:

class Wrapper
{
    [JsonProperty("JsonValues")]
    public ValueSet ValueSet { get; set; }
}

class ValueSet
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("values")]
    public Dictionary<string, Value> Values { get; set; }
}

class Value
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("diaplayName")]
    public string DisplayName { get; set; }
}

您需要反序列化到 Wrapper 类,而不是 ValueSet 类.然后,您可以从 Wrapper 中获取 ValueSet.

You need to deserialize into the Wrapper class, not the ValueSet class. You can then get the ValueSet from the Wrapper.

var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

这是一个演示的工作程序:

Here is a working program to demonstrate:

class Program
{
    static void Main(string[] args)
    {
        string jsonString = @"
        {
            ""JsonValues"": {
                ""id"": ""MyID"",
                ""values"": {
                    ""value1"": {
                        ""id"": ""100"",
                        ""diaplayName"": ""MyValue1""
                    },
                    ""value2"": {
                        ""id"": ""200"",
                        ""diaplayName"": ""MyValue2""
                    }
                }
            }
        }";

        var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;

        Console.WriteLine("id: " + valueSet.Id);
        foreach (KeyValuePair<string, Value> kvp in valueSet.Values)
        {
            Console.WriteLine(kvp.Key + " id: " + kvp.Value.Id);
            Console.WriteLine(kvp.Key + " name: " + kvp.Value.DisplayName);
        }
    }
}

这是输出:

id: MyID
value1 id: 100
value1 name: MyValue1
value2 id: 200
value2 name: MyValue2