【问题标题】:The JSON value could not be converted to System.Collections.Generic.ListJSON 值无法转换为 System.Collections.Generic.List
【发布时间】:2021-07-06 17:33:27
【问题描述】:

我正在尝试列出游戏中的所有物品using System.Text.Json; 我对使用 .json 文件很陌生 我试过这样做来测试它是否有效:

            List<Item> AllItems = new List<Item>();
            string json = File.ReadAllText("Items.json");
            AllItems = Deserialize<List<Item>>(json);
            WriteLine(AllItems[0].Name);

它不允许我将其转换为列表 这是json文件

{
  "Item": [
    {
      "Weapon": [
        {
          "CritChance": 60,
          "HitChance": 80,
          "OGDamge": 15,
          "Damage": 15,
          "Damage_Type": "Physical",
          "Weapon_Type": "Fist",
          "Player": null,
          "APUsage": 4,
          "ID": 1,
          "Name": "Debug",
          "Value": 16,
          "Rarity": "Common",
          "Item_Type": "Weapon"
        },
        {
          "CritChance": 40,
          "HitChance": 70,
          "OGDamge": 15,
          "Damage": 15,
          "Damage_Type": "Electric",
          "Weapon_Type": "Bow",
          "Player": null,
          "APUsage": 5,
          "ID": 2,
          "Name": "Debug2",
          "Value": 15,
          "Rarity": "Common",
          "Item_Type": "Weapon"
        }
      ]
    }
  ]
}

谁能帮帮我:)

【问题讨论】:

  • 您需要创建代表该 json 结构的类。查看QuickType;你至少应该有一个Item 和Weapon 类。您还必须决定将哪种类型的Player 考虑为null。
  • 您的 List 必须是一个具有名为“Item”的属性的类,它是一个 List
  • @zaggler 我该怎么做,哈哈,我真的没那么先进
  • @PrinceJKB 请查看我发布的链接,获取您的 json 并将其粘贴到编辑器中,它将根据该结构为您制作必要的类。
  • @zaggler 我应该将它粘贴到 Visual Studio 或 json 文件中

标签: c# json generic-list


【解决方案1】:

您的 json 文件以花括号开头,这意味着您需要包含名为 Item 的 Item 列表的父类, 或者您只是更改您的 json 文件,以 [ 括号开头 这意味着您的主要对象是集合。

【讨论】:

    【解决方案2】:

    下面的代码不使用您使用的文件,而是使用(几乎)内容相同的字符串。在这种情况下,您需要为 Json 中的每个项目声明类,然后才能使用 NewtonSoft 进行反序列化。

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    
    namespace JsonDeserialize
    {
        class Program
        {
            static void Main(string[] args)
            {
                var json = @"
    {
      'Items': [
        {
          'Weapons': [
            {
              'CritChance': 60,
              'HitChance': 80,
              'OGDamge': 15,
              'Damage': 15,
              'Damage_Type': 'Physical',
              'Weapon_Type': 'Fist',
              'Player': null,
              'APUsage': 4,
              'ID': 1,
              'Name': 'Debug',
              'Value': 16,
              'Rarity': 'Common',
              'Item_Type': 'Weapon'
            },
            {
              'CritChance': 40,
              'HitChance': 70,
              'OGDamge': 15,
              'Damage': 15,
              'Damage_Type': 'Electric',
              'Weapon_Type': 'Bow',
              'Player': null,
              'APUsage': 5,
              'ID': 2,
              'Name': 'Debug2',
              'Value': 15,
              'Rarity': 'Common',
              'Item_Type': 'Weapon'
            }
          ]
        }
      ]
    }";
    
                var allItems = JsonConvert.DeserializeObject<AllItems>(json);
                Console.WriteLine(allItems.Items[0].Weapons[0].Name);
                Console.ReadKey();
            }
        }
    
        public class AllItems
        {
            public List<Item> Items { get; set; }
        }
    
        public class Item
        {
            public List<Weapon> Weapons { get; set; }
        }
    
        public class Weapon
        {
            public int CritChance { get; set; }
            public int HitChance { get; set; }
            public int OGDamge { get; set; }
            public int Damage { get; set; }
            public string Damage_Type { get; set; }
            public string Weapon_Type { get; set; }
            public string Player { get; set; }
            public int APUsage { get; set; }
            public string ID { get; set; }
            public string Name { get; set; }
            public int Value { get; set; }
            public string Rarity { get; set; }
            public string Item_Type { get; set; }
        }
    }
    

    在这些情况下总是对我有帮助的是像这样使用“动态”关键字:

            dynamic allItems = JsonConvert.DeserializeObject<dynamic>(json);
    

    您可以使用'locals'查看反序列化的内容

    【讨论】:

    • 我仍然不知道该怎么做哈哈,我有一个物品类,武器是它的派生类
    • 你能看到类中的层次结构吗? Allitems 类有一个项目列表。物品有一个武器列表。这正是您在 Json.xml 中声明的内容。尝试阅读代码。你不明白代码的哪一部分?不要只是说我不明白。告诉我们你不明白的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多