Json data form of my project is like this.
[
{
"UnitName" : "A" ,
"Level" : "1" ,
"HitPoint" : "450" ,
"Damage" : "8" ,
"Experience" : "100"
},
{
"UnitName" : "A" ,
"Level" : "2" ,
"HitPoint" : "540" ,
"Damage" : "11" ,
"Experience" : "150"
},
....
]
I tried to read this using JsonUtility. This is my reading Json code.
public void Load()
{
string jsonString = File.ReadAllText(Application.dataPath + "/Database/SpiritDataA.json");
CharacterData data = JsonUtility.FromJson<CharacterData>(jsonString);
Debug.Log(data);
}
I was test it, and get this error.
ArgumentException: JSON must represent an object type.
I tried by other method that using in array.
[Serializable]
public class Wrapper<T>
{
public T[] items;
}
public class JsonHelper
{
public void Load()
{
string jsonString = File.ReadAllText(Application.dataPath + "/Database/SpiritData.json");
CharacterData data = JsonUtility.FromJson<CharacterData>(jsonString);
Debug.Log(data);
}
public CharacterData[] FromJson(string s)
{
return JsonUtility.FromJson<Wrapper<CharacterData>>(s).items;
}
}
I get same error.
As a result of a Google search, I think that this file structure is a bit different from other common things. but I want to read the file, preserving the existing format if possible.
How can i do?