- Home /
Question by
Angeluserare · Apr 06, 2018 at 06:36 AM ·
c#unity 5json
Help me with this json code please
I'm getting a " JSON must represent an object type." error at Line 14. I still couldn't figure out what's wrong.
public class JsonDemo : MonoBehaviour
{
string path;
string jsonString;
private void Start()
{
path = Application.streamingAssetsPath + "/Creature.json";
jsonString = File.ReadAllText(path);
print(path);
print(jsonString);
Creature Yumo = JsonUtility.FromJson<Creature>(jsonString);
Debug.Log(Yumo);
}
}
[System.Serializable]
public class Creature
{
public string Name;
public int Level;
public int[] Stats;
}
and json is:
{
"Name" :"Yumo",
"Level":"7",
"Stats":[4,7]
}
Comment
Best Answer
Answer by Hellium · Apr 06, 2018 at 06:50 AM
Have you tried to serialize a Creature into JSON and compare the results?
In your data structure, Level
is an integer, but, in your JSON, the level is a string.
Yea problem was with the json file. I deleted and recreated it and it fixed it. Thanks for ther reply.