Search Unity

LitJson asking me to initialize the Json data before running script

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

  1. CorruptedDev

    CorruptedDev

    Joined:
    Jan 17, 2016
    Posts:
    6
    I'm getting this error:
    InvalidOperationException: The JsonData instance has to be initialized first
    LitJson.JsonData.EnsureCollection ()
    LitJson.JsonData.get_Count ()
    ItemDatabase.ConstructItemDatabase () (at Assets/Scripts/ItemDatabase.cs:20)
    ItemDatabase.Start () (at Assets/Scripts/ItemDatabase.cs:14)
    this is my c# script
    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.         Debug.Log (database [1].Title);
    16.     }
    17.  
    18.     void ConstructItemDatabase()
    19.     {
    20.         for (int i = 0; i < itemData.Count; i++) {
    21.             database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"]));
    22.         }
    23.     }
    24. }
    25.  
    26. public class Item
    27. {
    28.     public int ID{ get; set;}
    29.     public string Title{ get; set;}
    30.     public int Value { get; set;}
    31.  
    32.     public Item(int id, string title, int value ){
    33.         this.ID = id;
    34.         this.Title = title;
    35.         this.Value = value;
    36.        
    37.     }
    38. }
     
  2. jeremyrjk

    jeremyrjk

    Joined:
    Feb 14, 2016
    Posts:
    1
    Did you figure this out? I have similar code and have a different error:

    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-runtime-and-classlibs/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:29)
    ItemDatabase.Start () (at Assets/Scripts/ItemDatabase.cs:14)
     
  3. Adnan_91

    Adnan_91

    Joined:
    Nov 28, 2013
    Posts:
    8
    Hi, guys did you find an answer for LitJson and InvalidOperationException: The JsonData instance has to be initialized first?
     
  4. Vhaldir

    Vhaldir

    Joined:
    Jan 26, 2019
    Posts:
    1
    I know this is an old thread, but I just ran into the same issue while working with Unity and LitJson (and also branching from the above referenced item database tutorial)

    The issue that I found/corrected, is that I had somehow overwritten my items.json contents to be blank; and so the ConstructItemDatabase method was unable to reference the data (basically the same as 'Object not set to an instance of an object' for any regular object)..

    Check to make sure your items.json is populated, or add error handling.

    Hope this helps!