- Home /
Question by
Ivangriga1 · Sep 27, 2017 at 05:52 PM ·
c#unity 5json
KeyNotFoundException: The given key was not present in the dictionary.
Im a new unity user and i was trying to make a simple inventory system with a tutorial. I have seen other questions about this and fixes, and it is all about a mistake or a missing coma, but i have scanned my JSON a few times and i cant find it.
[
{
"id": 0,
"title": "Pistol",
"weight": 1,
"damage": 25
}
{
"id": 1,
"title": "Magnum",
"weight": 2,
"damage": 32
}
{
"id": 2,
"title": "SniperRifle",
"weight": 4,
"damage": 195
}
{
"id": 3,
"title": "AK",
"weight": 3,
"damage": 38
}
{
"id": 4,
"title": "PistolRounds",
"weight": 0
"damage": 0
}
{
"id": 5,
"title": "MagnumRounds",
"weight": 0
"damage": 0
}
{
"id": 6,
"title": "SniperRounds",
"weight": 0
"damage": 0
}
{
"id": 7,
"title": "AKRounds",
"weight": 0
"damage": 0
}
{
"id": 8,
"title": "WoodLog",
"weight": 1
"damage": 0
}
{
"id": 9,
"title": "Stone",
"weight": 1
"damage": 0
}
]
Here is the error:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.Int32,System.Int32[]].get_Item (Int32 key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/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.ToWrapper (LitJson.WrapperFactory factory, System.String json)
LitJson.JsonMapper.ToObject (System.String json)
itemDatabase.Start () (at Assets/Inventory/Scripts/itemDatabase.cs:13)
Here is my c# item database script.
using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;
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].Title);
}
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]["damage"], (int)itemData[i]["weight"]));
}
}
}
public class Item
{
public int ID { get; set; }
public string Title { get; set; }
public int Damage { get; set; }
public int Weight { get; set; }
public Item(int id, string title, int damage, int weight)
{
this.ID = id;
this.Title = title;
this.Damage = damage;
this.Weight = weight;
}
}
All help will be appreciated.
Comment
Best Answer
Answer by unit_nick · Sep 28, 2017 at 07:08 PM
[{
"id": 0,
"title": "Pistol",
"weight": 1,
"damage": 25
},
{
"id": 1,
"title": "Magnum",
"weight": 2,
"damage": 32
},
{
"id": 2,
"title": "SniperRifle",
"weight": 4,
"damage": 195
},
{
"id": 3,
"title": "AK",
"weight": 3,
"damage": 38
},
{
"id": 4,
"title": "PistolRounds",
"weight": 0,
"damage": 0
},
{
"id": 5,
"title": "MagnumRounds",
"weight": 0,
"damage": 0
},
{
"id": 6,
"title": "SniperRounds",
"weight": 0,
"damage": 0
},
{
"id": 7,
"title": "AKRounds",
"weight": 0,
"damage": 0
},
{
"id": 8,
"title": "WoodLog",
"weight": 1,
"damage": 0
},
{
"id": 9,
"title": "Stone",
"weight": 1,
"damage": 0
}]
I believe what the answer is pointing out is that you're missing a few commas.
Answer by AscendDev · Nov 08, 2017 at 02:09 PM
@Ivangriga1 Just to point out that you can verify your JSON at http://jsonlint.com.