PrinceJKB
PrinceJKB

Reputation: 123

The JSON value could not be converted to System.Collections.Generic.List

I am trying to make a list of all items in the game using System.Text.Json; I am very new to using .json files I have tried doing this to test if it works:

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

It doesnt let me Convert It into a list This is the json file

{
  "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"
        }
      ]
    }
  ]
}

Can anyone help me out :)

Upvotes: 11

Views: 57527

Answers (2)

Paul Sinnema
Paul Sinnema

Reputation: 2792

The code below does not use the file you use but a string with (almost) the same contents. You need to declare classes for every item in your Json before you can deserialize with in this case 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; }
    }
}

What always helps me in these cases is using the 'dynamic' keyword like so:

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

You can see what is deserialized using the 'locals'

enter image description here

Upvotes: 3

amuratgencay
amuratgencay

Reputation: 186

Your json file starts with curly braces that means you need parent class that contains list of Item named Item, Or you just change your json file, starts with [ bracket that means your main object is collection.

Upvotes: 16

Related Questions