【问题标题】:Deserialzation from JSON file with multiple objects in C#C#中具有多个对象的JSON文件的反序列​​化
【发布时间】:2021-06-15 20:14:50
【问题描述】:

我有一个 JSON 文件 people.json 包含多个对象,我想将这些对象解析为包含 Person 对象但没有运气的列表 People。我从 peopleLis 得到 null ,我怀疑我在这里做错了什么(可能很多)。你们能帮帮我吗?

{
    "Andrew": {
        "weight": "75",
        "height": "181"
    },
    "Nathalie": {
        "weight": "68",
        "height": "182"
    },
    "Dave": {
        "weight": "83",
        "height": "192"
    }
}

这是我试过的代码:

        public class Person
        {
            public int weight { get; set; }
            public int height { get; set; }
        }

        public class People
        {
            public List<Person> people { get; set; }
        }

        static void Main(string[] args)
        {
            People peopleList = JsonConvert.DeserializeObject<People>(File.ReadAllText(@"C:\people.json"));
        }

【问题讨论】:

  • 作为问题所在的线索,您可以将 JSON 放入 this page,然后查看生成的类。
  • json 应该以 [ 开头并以 ] 结尾,这是一个对象列表。目前你有 {, }..

标签: c# json parsing json.net


【解决方案1】:

您的 JSON 不代表人的列表 - 它代表具有多个属性的单个对象,其中每个属性值都是一个人。

您可以将其反序列化为Dictionary&lt;string, Person&gt;

string json = File.ReadAllText(@"C:\people.json");
var people = JsonConvert.DeserializeObject<Dictionary<string, Person>>(json);

如果需要,您可以从中创建一个列表(例如 people.Values.ToList()),但您不应依赖于该列表的顺序与 JSON 文件中的顺序相同 - 从根本上说,JSON 对象属性并非旨在对订单敏感。鉴于此人的姓名可能很重要,我会坚持使用字典,其中每个条目的键都是姓名。

(作为旁注,我建议对属性使用惯用名称,例如 WeightHeight,然后在需要时使用 [JsonProperty] 指定 JSON 名称。)

【讨论】:

    【解决方案2】:

    这里有一些事情没有解决:

    1. 如上所述,JSON 不代表 List 结构,它实际上是 3 个不同的对象,分别名为 Andrew、Nathalie 和 Dave
    2. 上面的“人”类也必须在 JSON 中表示 - 现在不是。
    3. 也许需要另一个名为“姓名”的字段,就像“身高”和“体重”一样
    4. 对于存储值的类(我怀疑您需要:)您将需要成员变量,而不仅仅是属性函数

    我不知道你是否可以更改 JSON 或/和代码,但这里有一段代码和一个 JSON 可能对你有用 - 我没有尝试过,但你我想你明白了

    public class Person
    {
        private string _name;
        private int _weight;
        private int _height;
    
    
        public string Name { get => _name; set => _name = value; }
        public int Height { get => _height; set => _height = value; }
    
        public int Weight { get => _weight; set => _weight = value; }
    }
    
    public class PeopleList
    {
        private List<Person> people;
    
        public List<Person> People { get => people; set => people = value; }
    }
    
    static void Main(string[] args)
    {
        PeopleList peopleList = JsonConvert.DeserializeObject<People>(File.ReadAllText(@"C:\people.json"));
    }
    

    这是 JSON

    {
      "People": [
        {
          "Name": "Andrew",
          "Weight": "75",
          "Height": "181"
        },
        {
          "Name": "Nathalie",
          "Weight": "68",
          "Height": "182"
        },
        {
          "Name": "Dave",
          "Weight": "83",
          "Height": "192"
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多