将嵌套的JSON反序列化为C#类

时间:2018-09-12 20:44:52

标签: c# json serialization

我有一个像这样的JSON类

{
    "Items": {
        "Item_1A": {
            "prop1": "string",
            "prop2": "string",
            "prop3": 1,
            "prop4": [{
                "prop_x": 100
            },
            {
                "prop_y": 200
            }]
        },
        "Item2B": {
            "prop1": "string",
            "prop2": "string",
            "prop3": 14,
            "prop4": [{
                "prop_z": 300
            }]
        }
    }
}

如何将其纳入C#类?这是我到目前为止的内容:

public class Info
{
    public string prop1 {get;set;}
    public string prop2 {get;set;}
    public int prop3 {get;set;}
    public Dictionary<string, List<int>> prop4 {get;set;}
}
public class Response
{
    public Dictionary<string, List<Info>> Item {get;set;}
}

我尝试点击此链接,但是没有用 Deserialize nested JSON into C# objects

4 个答案:

答案 0 :(得分:0)

以下是在json之后创建代理类的提示:

首先,要确保它是有效的JSON,请访问此网站并运行验证程序:https://jsonlint.com/

如果可以,有一些工具可以将json和xml直接转换为c#代理类,例如:http://json2csharp.com/https://xmltocsharp.azurewebsites.net/

现在就去!只需将它们复制到您的模型中并开始使用它即可。

答案 1 :(得分:0)

只需对模型进行一些小的更改。您的prop4则是一个列表或数组。

public class Info
{
  public string prop1 {get;set;}
  public string prop2 {get;set;}
  public int prop3 {get;set;}
  public List<Dictionary<string, List<int>>> prop4 {get;set}
}
public class  Response
{
  public class Dictionary<string, Info> Items {get;set;} // Should be named Items
}

答案 2 :(得分:0)

您的prop4Dictionary<string, List<int>>,在序列化时看起来像这样(为简洁起见,我在发出的JSON中省略了$type属性):

{
   "X": {
      "$values": [ 1, 2 ]
   },
   "Y": {
      "$values": [ 3, 4 ]
   }    
}

您的JSON格式正确,但“形状”错误。它看起来更像一个包含一项字典的数组。

在上面的JSON中,XY是字典中的键,该字典是以JSON建模的“常规对象”。 $values代表每个键的整数列表。

答案 3 :(得分:0)

您的数据模型应如下:

public class Info
{
    public string prop1 {get;set;}
    public string prop2 {get;set;}
    public int prop3 {get;set;}
    // Modified from 
    //public Dictionary<string, List<int>> prop4 {get;set}
    public List<Dictionary<string, int>> prop4 {get;set;}
}

public class Response
{
    // Modified from 
    //public class Dictionary<string, List<Info>> Item {get;set;}
    public Dictionary<string, Info> Items {get;set;}
}

注意:

  • Response.Item应该被命名为Items

  • 在您的JSON中,"Items"是一个具有可变名称的对象值属性的对象:

    {
        "Items": {
            "Item_1A": { },
            "Item2B": { }
        }
    }
    

    对于适当的非集合类型Dictionary<string, T>,应将其建模为T。假设您正在使用,请参见Serialization Guide: Dictionaries了解详细信息。否则,的行为类似。

    您的数据模型public class Dictionary<string, List<Info>> Items适用于具有可变名称数组值属性的对象:

    {
        "Items": {
            "Item_1A": [{ },{ }],
            "Item2B": [{ }]
        }
    }
    

    但这不是您所拥有的。

  • 同时"prop4"是一个数组,其中包含具有可变名称的对象值属性的对象,例如:

    "prop4": [ // Outer container is an array
      {        // Inner container is an object
        "prop_x": 100
      },
      {
        "prop_y": 200
      }
    ]
    

    因此,应将诸如List<T>之类的集合用作外部通用类型:

    public List<Dictionary<string, int>> prop4 {get;set;}
    

    请参见Serialization Guide: IEnumerable, Lists, and Arrays

  • 您已经注意到,诸如 How to auto-generate a C# class file from a JSON object string 中提到的那些代码生成工具通常无法识别具有可变属性名称的JSON对象。在这种情况下,可能需要使用包含类型中的相应Dictionary<string, T>属性来手动替换自动生成的类。

示例工作提琴here

相关问题