The question is answered, right answer was accepted
Getting KeyNotFoundException and I can't understand why
Hello Unity Community!
I'm trying to make an inventory system for my game, so I've read a bit on the subject and now I'm following a tutorial of using LitJson in Unity. However, when running my code I get "KeyNotFoundException: The given key was not present in the dictionary.".
Here is my code:
 using UnityEngine;
 using System.Collections;
 using LitJson;
 using System.Collections.Generic;
 using System.IO;
 
 public class script_ItemDatabase : MonoBehaviour {
     private List<Item> database = new List<Item>();
     JsonData itemData;
 
     void Start()
     {
         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath+"/StreamingAssets/Items.json"));
         ConstructItemDatabase();
     }
 
     public Item GetItemByID(int index)
     {
         for(int i = 0; i < database.Count; i++)
         {
             if(database[i].ID==index)
             return database[i];
         }
         return null;
         
     }
 
     void ConstructItemDatabase()
     {
         for(int i = 0; i < itemData.Count; i++)
         {
             database.Add(new Item((int)itemData[i]["id"],
                                    itemData[i]["name"].ToString(),
                                    itemData[i]["description"].ToString(),
                                    (int)itemData[i]["damage"],
                                    (int)itemData[i]["firerate"],
                                    (bool)itemData[i]["stackable"],
                                    itemData[i]["slug"].ToString()));
         }
     }
 
 }
And here's my json:
 {
     {
         "id":0,
         "name":"Test Gun",
         "description":"It's a gun.",
         "damage":3,
         "firerate":2,
         "stackable":false,
         "slug":"duck_yellow"
     }
 }
The error shows up on the itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath+"/StreamingAssets/Items.json")); line. Since I'm new to this subject I would like to understand why the error shows up, what does it actually mean, and, of course, how can I fix it? Am I using the library incorrectly? Did I set up my json improperly?
I know there's a lot of questions and answers on the web regarding this error, but so far I don't have enough experience using dictionaries to fix my code on my own.
Thanks for your time!
-Ashky
Answer by Bunny83 · May 07, 2016 at 11:42 PM
This is not a valid JSON notation. The curly brackets represents an object. Objects always contain key-value pairs. It looks like you want an array with several objects in there, so your JSON representation should look like this;
  [
      {
          "id":0,
          "name":"Test Gun",
          "description":"It's a gun.",
          "damage":3,
          "firerate":2,
          "stackable":false,
          "slug":"duck_yellow"
      }
  ]
Note the square brackets which represents an array.
No problem ^^. Actually i recently work a lot with LUA and this could have happend to me as well. In LUA there is only one complex type: "table". It can represent both, an array or an object and has "almost" the same syntax as JSON.
 -- LUA
 {
    {
       id = 0,
       name = "Name1",
    },
    {
       id = 1,
       name = "Name2",
    }
 }
 
 // JSON
 [
    {
       "id" : 0,
       "name" : "Name1",
    },
    {
       "id" : 1,
       "name" : "Name2",
    }
 ]
As you can see, quite similar. JSON uses a colon for the key-value pairs, a LUA table uses the equals sign.
Follow this Question
Related Questions
Unity Inventory Database ID Error 1 Answer
SHA512 JS -> C# 0 Answers
Insert string into empty list at a specific index 0 Answers
Deserialize Json array with mixed types 0 Answers
Json to array of objects C# 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                