11

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 :)

7
  • You'll need to create classes that represent that json structure. Check out QuickType; you should at least have an Item and Weapon class. You will also have to decide what type of Player is going to be considering it's null. Commented Jul 6, 2021 at 17:37
  • Your List<Item> has to be a class with a property named "Item" which is a List<Item> Commented Jul 6, 2021 at 17:38
  • 1
    @zaggler how exactly would i do that lol, im not that advanced really Commented Jul 6, 2021 at 17:39
  • 1
    @PrinceJKB please see the link I posted, take your json and paste it in the editor, it will make the necessary classes based on that structure for you. Commented Jul 6, 2021 at 17:40
  • 1
    Your JSON cannot be serialized to a List<>. It contains a JSON object with a property called Item, which then contains a list. Commented Jul 6, 2021 at 17:44

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

i still have no idea what to do lol, i have an item class and weapon is a derived class from it
Can you see the hierarchy that is in the classes. The Allitems class has a list of Items. The Items have a list of Weapons. That is exactly what you declared in your Json. Try and read the code. What part of the code do you not understand? Don't just say I don't understand. Tell us what you don't understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.