Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LitJson and my inventory code are giving me an error

Discussion in 'Scripting' started by CorruptedDev, Feb 13, 2016.

  1. CorruptedDev

    CorruptedDev

    Joined:
    Jan 17, 2016
    Posts:
    6
    The error I am receiving is:
    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/Scripts/ItemDatabase.cs:13)
    my code is :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LitJson;
    4. using System.Collections.Generic;
    5. using System.IO;
    6.  
    7. public class ItemDatabase : MonoBehaviour {
    8.     private List <Item> database = new List<Item>();
    9.     private JsonData itemData;
    10.  
    11.     void Start()
    12.     {
    13.         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.Json"));
    14.         ConstructItemDatabase ();
    15.     }
    16.  
    17.     void ConstructItemDatabase()
    18.     {
    19.         for (int i = 0; i < itemData.Count; i++) {
    20.             database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"]));
    21.         }
    22.     }
    23. }
    24.  
    25. public class Item
    26. {
    27.     public int ID{ get; set;}
    28.     public string Title{ get; set;}
    29.     public int Value { get; set;}
    30.  
    31.     public Item(int id, string title, int value ){
    32.         this.ID = id;
    33.         this.Title = title;
    34.         this.Value = value;
    35.        
    36.     }
    37. }