Reputation: 41
So I wanted to create an inventory system and I have coded and done a few things. However now it is showing an error in the item database code. The error is:
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary
2[System.Int32,System.Collections.Generic.IDictionary
2[System.Int32,System.Int32[]]].get_Item (Int32 key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) LitJson.JsonReader.Read () Rethrow as JsonException: Invalid token '123' in input string LitJson.JsonReader.Read () LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader) LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader) LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader) LitJson.JsonMapper.ToWrapper (LitJson.WrapperFactory factory, System.String json) LitJson.JsonMapper.ToObject (System.String json) ItemDatabase.Start () (at Assets/All/Scripts/ItemDatabase.cs:13)
and it became very annoying since I couldn't figure out how to fix it. My 2 files currently are:
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine;
using System.IO;
public class ItemDatabase : MonoBehaviour {
private List<Item> database = new List<Item>();
private JsonData itemData;
void Start()
{
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
ConstructItemDatabase();
Debug.Log(database[1].Slug);
}
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]["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 Power { get; set; }
public int Defence { 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.Power = power;
this.Defence = defence;
this.Vitality = vitality;
this.Description = description;
this.Stackable = stackable;
this.Rarity = rarity;
this.Slug = slug;
}
public Item()
{
this.ID = -1;
}
}
Here the top one is my ItemDatabase.cs file. It has a few spaces and brackets are wrong I know but it is actually all fine. I will post a screenshot with the whole code. Since I am new to using this Code Sample tool.
And here is my Items.json file.
[
{
"id": 0,
"title": "Wood Pickaxe",
"value": 6,
"stats" {
"power": 1,
"defence": 0,
"vitality": 0
},
"description": "A basic wood pickaxe.",
"stackable": false,
"rarity": 1,
"slug": "wood_pickaxe"
},
{
"id": 1,
"title": "Wood Pickaxe Head",
"value": 543,
"stats" {
"power": 1,
"defence": 0,
"vitality": 0
},
"description": "A piece of a broken wood pickaxe.",
"stackable": false,
"rarity": 1,
"slug": "wood_pickaxe_head"
}
]
I will also post a screenshot of this code. Please help. This is very frustrating and the other posts about this won't help. Items.json screenshot ItemDatabase.cs screenshot
Upvotes: 0
Views: 314
Reputation: 743
You have an error in your json. The error itself is a little misleading. Every time I've seen it, there was a json syntax issue involved.
[
{
"id": 0,
"title": "Wood Pickaxe",
"value": 6,
"stats": { // <-- problem was here. missing colon
"power": 1,
"defence": 0,
"vitality": 0
},
"description": "A basic wood pickaxe.",
"stackable": false,
"rarity": 1,
"slug": "wood_pickaxe"
},
{
"id": 1,
"title": "Wood Pickaxe Head",
"value": 543,
"stats": { // <-- problem was here. missing colon
"power": 1,
"defence": 0,
"vitality": 0
},
"description": "A piece of a broken wood pickaxe.",
"stackable": false,
"rarity": 1,
"slug": "wood_pickaxe_head"
}
]
Upvotes: 1