2

Hey guys having trouble implementing the following database for Unity with Json from a tutorial, been searching for hours but can't seem to locate the problem

Here's my code: ItemDatabase.cs

public class ItemDatabase : MonoBehaviour {
    private List<Item> database = new List<Item>(); // Json database
    private JsonData itemData;

    void Start()
    {
        itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.Json")); // change from json object to be readible by c#
        ConstructItemDatabase();

        Debug.Log(database[1].Value);
    }

    void ConstructItemDatabase()
    {
        for (int i = 0; i < itemData.Count; i++)
        {
            database.Add(new Item(
                (int)itemData[i]["id"],
                itemData[i]["title"].ToString(),
                (int)itemData[i]["stats"]["value"],
                (int)itemData[i]["stats"]["power"],
                (int)itemData[i]["stats"]["defence"],
                (int)itemData[i]["stats"]["vitality"],
                itemData[i]["description"].ToString(),
                (bool)itemData[i]["stackable"],
                (int)itemData[i]["rarity"],
                itemData[i]["slug"].ToString()));
        }
    }

}

public class Item
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public int Value { get; set; }
        public int Defence { get; set; }
        public int Power { get; set; }
        public int Vitality { get; set; }
        public string Description { get; set; }
        public bool Stackable { get; set; }
        public int Rarity { get; set; }
        public string Slug { get; set; }

    public Item(int id, string title, int value, int power , int defence, int vitality, string description, bool stackable, int rarity, string slug)
        {
            this.ID = id;
            this.Title = title;
            this.Value = value;
            this.Defence = defence;
            this.Power = power;
            this.Vitality = vitality;
            this.Description = description;
            this.Stackable = stackable;
            this.Rarity = rarity;
            this.Slug = slug;

        }

    public Item()
        {
            this.ID = -1;
        }
    }

and my code for the json file Items.json

[
    {
        "id": 0,
        "title": "Steel Gloves",
        "value": 6,
        "stats": {
            "power": 1,
            "defence": 4,
            "vitality": 2
        },
        "description": "Gloves with steel plating",
        "stackable": false,
        "rarity": 2,
        "slug": "steel_gloves"
    },
    {
        "id": 1,
        "title": "The Great Stick",
        "value": 543,
        "stats": {
            "power": 56,
            "defence": 2,
            "vitality": 54
        },
        "description": "A Stick that's pretty great",
        "stackable": true,
        "rarity": 6,
        "slug": "the_great_stick"
    }
]

When I debug the program from visual studio it works just fine however in Unity when I hit play I get some errors:

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,LitJson.JsonData].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
LitJson.JsonData.get_Item (System.String prop_name)
ItemDatabase.ConstructItemDatabase () (at Assets/Scripts/ItemDatabase.cs:27)
ItemDatabase.Start () (at Assets/Scripts/ItemDatabase.cs:15)

I was thinking I spelled one of the strings wrong in the ContructItemDatabase() but I feel like I've checked over it 100 times.

Any help is appreciated thanks

Mohit S
  • 13,723
  • 6
  • 34
  • 69

1 Answers1

4

Your Class should look like this

public class Stats
{
    public int power { get; set; }
    public int defence { get; set; }
    public int vitality { get; set; }
}

public class items
{
    public int id { get; set; }
    public string title { get; set; }
    public int value { get; set; }
    public Stats stats { get; set; }
    public string description { get; set; }
    public bool stackable { get; set; }
    public int rarity { get; set; }
    public string slug { get; set; }
}

because the "power", "defence", "vitality" is under "stats".

Secondly I'll recommened you to use Newtonsoft to get this kind of JSON into objects. Which I believe is very easy to understand.

Mohit S
  • 13,723
  • 6
  • 34
  • 69