Unity中的新JSonUtitilty/JSonHelper有问题,当它应该是一个类时返回null [英] Having issues with new JSonUtitilty/JSonHelper in Unity, returning null when it should be a class
本文介绍了Unity中的新JSonUtitilty/JSonHelper有问题,当它应该是一个类时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过遵循以下步骤创建了JSonHelper类: http://www.boxheadproductions.com. au/deserializing-top-level-arrays-in-json-with-unity/
I created the JSonHelper class by following along here: http://www.boxheadproductions.com.au/deserializing-top-level-arrays-in-json-with-unity/
,并具有以下json文件:
and have a json file as follows:
{"Items":[{"id":0,"name":"player","baseStats":[10,0,0,0,0,0],"xpyield":0,"evyield":[0,0,0,0,0,0],"moves":["Ember","","",""],"health":0,"stats":[0,0,0,0,0,0],"evs":[0,0,0,0,0,0],"level":1,"xp":0},{"id":1,"name":"frosline","baseStats":[10,0,0,0,0,0],"xpyield":0,"evyield":[0,0,0,0,0,0],"moves":["Ember","Blast","",""],"health":0,"stats":[0,0,0,0,0,0],"evs":[0,0,0,0,0,0],"level":1,"xp":0}]}
还有一个MonsterStats类:
Also have a MonsterStats Class:
public class MonsterStats
{
public int id;
public string name;
public int[] baseStats;
public int xpyield = 0;//0-4 for each stat
public int[] evyield = new int[] { 0, 0, 0, 0, 0, 0 };
public string[] moves;
public int health;
public int[] stats;
public int[] evs;
public int level;
public int xp;
}
最后是我遇到问题的代码:
And finally the code where I'm having issues:
jsonString = File.ReadAllText(Application.dataPath + "/Resources/Monsters.json");
monsterData = JsonHelper.FromJson<MonsterStats>(jsonString);
Debug.Log(monsterData);
此Debug.Log返回null,有什么想法我做错了吗?
This Debug.Log returns null, any ideas where I've gone wrong?
推荐答案
只是缺少[System.Serializable]或MonsterStats之前的任何内容
Was simply missing [System.Serializable] or whatever before MonsterStats
这篇关于Unity中的新JSonUtitilty/JSonHelper有问题,当它应该是一个类时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文