- Home /
LitJson not working correctly
Hello, I'm having a issue with using litjson, I have referenced my litjson file in visual studios and made sure i have placed it into my plugins folders, yet it's saying, 'The type or namespace name `LitJson' could not be found. Are you missing an assembly reference?" I have the following code:
using System.Collections.Generic; using UnityEngine; using LitJson; using System.IO;
public class ItemDatabase : MonoBehaviour { private List database = new List(); public LitJson.JsonData itemData;
void Start()
{
itemData = LitJson.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]["value"], (int)itemData[i]["weight"]));
}
}
}
public class Item { public int ID { get; set; } public string Title { get; set; } public int Value { get; set; } public int Weight { get; set; }
public Item(int id, string title, int value, int weight)
{
this.ID = id;
this.Title = title;
this.Value = value;
this.Weight = weight;
}
}
Thanks for any help
Answer by Firas4d · Apr 16, 2018 at 06:55 AM
Hi, I encourage you to use Unity serialization instead of using any other solution actually. It's built-in, easy to use and well documented.
Answer by Bunny83 · Apr 16, 2018 at 07:28 AM
You can not add assembly references inside the VS project as the VS project is only used for synchronisation between Unity and VS. Any external assembly you want to use in your project has to physically exist inside the assets folder. Unity will setup the references automatically once you copied the assembly into the assets folder. Keep in mind that Unity does not use the .NET environment which might be installed on your system but uses a Mono runtime that is built into your final game. That's why everything need to be shipped with your game.
Your answer
Follow this Question
Related Questions
Parse JSON with LitJSON 2 Answers
How to read an int array using LitJson? 1 Answer
How to get object array from json? 0 Answers
Deserializing a JSON file yields 0 when referencing the variables in another script 2 Answers
Litjson autoformat 1 Answer