- Home /
Json not recognized on Android
So, I have this inventory system that reads in a json database and fills in item data. It's working fine in the editor but not on my android. Works fine in windows, but I have to manually go in and paste the json into the data folders. Any help on getting android to recognize this? The problem is definitely in android not finding the Json.
The top 3 commented out lines were what I was trying, but kept getting errors. I get NullReferenceException on the string content = text.ToString(); line.
void Start()
{
//text = Resources.Load<TextAsset>("Items.json");
//string content = text.ToString();
//itemData = JsonMapper.ToObject(File.ReadAllText(content));
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Resources/Items.json"));
ConstructItemDatabase();
}
public Item FetchItemByID(int id)
{
for (int i = 0; i < database.Count; i++)
{
if (database[i].ID == id)
{
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]["title"].ToString(), (int)itemData[i]["rarity"], itemData[i]["description"].ToString(), itemData[i]["classes"].ToString(),itemData[i]["spritelocation"].ToString(),(int)itemData[i]["equipslot"], itemData[i]["prefablocation"].ToString(),(bool)itemData[i]["stackable"]));
}
}
Answer by IvovdMarel · Jul 18, 2016 at 05:33 AM
Using Dictionaries to read out JSON objects is very common but it's much easier to serialize it to a struct or class (which is also built-in to Unity using JsonUtilty.FromJson and JsonUtility.ToJson). Besides being an easier way to handle JSON, this should work on all platforms.
void Start (){
string jsonString = File.ReadAllText(Application.dataPath + "/Resources/Items.json")));
MySpecificData data = JsonUtility.FromJson<MySpecificData>();
}
public class MySpecificData{
public int id;
public string title;
public int rarity;
public string description;
public string classes;
public string spritelocation;
public int equipslot;
public string prefablocation
public bool stackable;
}
Thanks for the reply! That worked, but I had to add a wrapper class for the JSON, as I read here: http://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/
Now I'm getting NullReferenceException on my ConstructItemDatabase() method.
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]["rarity"], itemData[i]["description"].ToString(), itemData[i]["classes"].ToString(),itemData[i]["spritelocation"].ToString(),(int)itemData[i]["equipslot"], itemData[i]["prefablocation"].ToString(),(bool)itemData[i]["stackable"]));
}
And here's the wrapper:
public class JsonHelper { public static T[] getJsonArray(string json) { string newJson = "{ \"array\": " + json + "}"; Wrapper wrapper = JsonUtility.FromJson (newJson); return wrapper.array; }
private class Wrapper<T>
{
public T[] array;
}
}
Try doing it without the generic. I'm not sure that would work right now.