JsonFx反序列化返回null unity3d

JsonFx反序列化返回null unity3d,unity3d,deserialization,json-deserialization,jsonfx,Unity3d,Deserialization,Json Deserialization,Jsonfx,当我试图使用JsonFx反序列化Json字符串时,我收到的值为null 这是我的Json字符串 { "PlayerID": 123, "PlayerName": "Avi", "DemiGods": { "Red": { "Name": "Red", "Level": 20, "Attack": 5, "Dodge": 1, "Defence": 10, "Speed": 10 }, "B

当我试图使用
JsonFx
反序列化
Json
字符串时,我收到的值为null

这是我的Json字符串

{
  "PlayerID": 123,
  "PlayerName": "Avi",
  "DemiGods": {
    "Red": {
      "Name": "Red",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Blue": {
      "Name": "Blue",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Green": {
      "Name": "Green",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Light": {
      "Name": "Light",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    },
    "Dark": {
      "Name": "Dark",
      "Level": 20,
      "Attack": 5,
      "Dodge": 1,
      "Defence": 10,
      "Speed": 10
    }
  },
  "PlayerGrid": {
    "Red": {
      "x": 0,
      "y": 1
    },
    "Blue": {
      "x": 1,
      "y": 1
    },
    "Green": {
      "x": 2,
      "y": 1
    },
    "Light": {
      "x": 2,
      "y": 2
    },
    "Dark": {
      "x": 3,
      "y": 2
    }
  },
  "AIGrid": {
    "Red": {
      "x": 0,
      "y": 1
    },
    "Blue": {
      "x": 1,
      "y": 1
    },
    "Green": {
      "x": 2,
      "y": 1
    },
    "Light": {
      "x": 2,
      "y": 2
    },
    "Dark": 
    {
      "x": 3,
      "y": 2
    }
  }
}

This is my class where i stores the data from Json

public class UnitsInfo :MonoBehaviour
{
    public string PlayerName;
    public int PlayerID;
    public List<DemiGods> demigodslist = new List<DemiGods>();
    public List<GridData> playerGridlist = new List<GridData>();
    public List<GridData> AIGridList = new List<GridData>();

    public UnitsInfo() 
    {
        Debug.Log("Default Constructor");
    }

    public UnitsInfo(string _name, int id, List<DemiGods> Godlist, List<GridData> plist, List<GridData> AIlist)
    {
        PlayerName = _name;
        PlayerID = id;
        demigodslist = Godlist;
        playerGridlist = plist;
        AIGridList = AIlist;
    }

    public class DemiGods
    {
        public string Name;
        public int Level;
        public float Attack;
        public float Dodge;
        public float Defence;
        public float Speed;
        public DemiGods() 
        {
            Debug.Log("DemIGOds DeFALUT ConsTruCtoR");
        }
        public DemiGods(string _name, int _lvl, float _attack, float _dodge, float _Defence, float _speed)
        {
            Name = _name;
            Level = _lvl;
            Attack = _attack;
            Dodge = _dodge;
            Defence = _Defence;
            Speed = _speed;
        }
    }

    public class GridData
    {
        public Vector2 pos;

        public GridData() { Debug.Log("Grid DAta DeFALUT ConsTruCtoR"); }
        public GridData(int x, int y)
        {
            pos.x = x;
            pos.y = y;
        }
    }
  }

This is where is deserialize data 
public class JsonData: MonoBehaviour 
{
    public string JSONString;
    void Start()
    {
        UnitsInfo HerosList = JsonReader.Deserialize<UnitsInfo>(JSONString);
        Debug.Log(HerosList);
    }
}

The Debug.log return null. Where im going wrong??

Thanks in Advance
{
“玩家ID”:123,
“PlayerName”:“Avi”,
“半神”:{
“红色”:{
“名称”:“红色”,
“级别”:20,
“攻击”:5,
“道奇”:1,
“辩护”:10,
“速度”:10
},
“蓝色”:{
“名称”:“蓝色”,
“级别”:20,
“攻击”:5,
“道奇”:1,
“辩护”:10,
“速度”:10
},
“绿色”:{
“名称”:“绿色”,
“级别”:20,
“攻击”:5,
“道奇”:1,
“辩护”:10,
“速度”:10
},
“光”:{
“名称”:“光”,
“级别”:20,
“攻击”:5,
“道奇”:1,
“辩护”:10,
“速度”:10
},
“黑暗”:{
“名称”:“黑暗”,
“级别”:20,
“攻击”:5,
“道奇”:1,
“辩护”:10,
“速度”:10
}
},
“PlayerGrid”:{
“红色”:{
“x”:0,
“y”:1
},
“蓝色”:{
“x”:1,
“y”:1
},
“绿色”:{
“x”:2,
“y”:1
},
“光”:{
“x”:2,
“y”:2
},
“黑暗”:{
“x”:3,
“y”:2
}
},
“艾格里德”:{
“红色”:{
“x”:0,
“y”:1
},
“蓝色”:{
“x”:1,
“y”:1
},
“绿色”:{
“x”:2,
“y”:1
},
“光”:{
“x”:2,
“y”:2
},
“黑暗”:
{
“x”:3,
“y”:2
}
}
}
这是我存储Json数据的类
公共类单位信息:单一行为
{
公共字符串播放器名称;
公共int PlayerID;
public List demigodslist=新列表();
public List playerGridlist=新列表();
公共列表AIGridList=新列表();
公共单位信息()
{
Log(“默认构造函数”);
}
public UnitsInfo(字符串_name,int id,列表Godlist,列表plist,列表AIlist)
{
PlayerName=\u名称;
PlayerID=id;
半神名单=神名单;
playerGridlist=plist;
AIGridList=AIlist;
}
公营半神
{
公共字符串名称;
公共智力水平;
公众浮标攻击;
公共浮动道奇;
公众浮标防御;
公众浮标速度;
公共半人神()
{
Log(“demilut构造函数”);
}
公共半神(字符串名称、智力等级、浮球攻击、浮球闪避、浮球防御、浮球速度)
{
名称=_名称;
级别=_lvl;
攻击=_攻击;
道奇=_道奇;
防御=_防御;
速度=_速度;
}
}
公共类网格数据
{
公共向量机2 pos;
公共GridData(){Debug.Log(“网格数据定义构造函数”);}
公共网格数据(整数x,整数y)
{
位置x=x;
位置y=y;
}
}
}
这就是反序列化数据的地方
公共类JsonData:单一行为
{
公共字符串JSONString;
void Start()
{
UnitsInfo HERSLIST=JsonReader.Deserialize(JSONString);
Debug.Log(HerosList);
}
}
Debug.log返回null。我哪里出错了??
提前谢谢

我认为您的json与您想要的不匹配。在你的情况下,我认为你期望红,蓝,绿是半神的物体,而红,蓝,绿是它们的名字

但正如json目前所做的那样,它希望红色、蓝色和绿色都是这种类型

    {
    "PlayerID": 123,
    "PlayerName": "Avi",
    "DemiGods": [{
        "Name": "Red",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Blue",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Green",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Light",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }, {
        "Name": "Dark",
        "Level": 20,
        "Attack": 5,
        "Dodge": 1,
        "Defence": 10,
        "Speed": 10
    }]
}
在上面的例子中,半神是半神类型的对象数组,名称包含在对象中。因此,您必须声明一个方法来获取,或者迭代数组,直到得到一个与名称匹配的对象,或者创建一个带有字符串的字典,等等

显然,Json文件的其余部分也是如此

编辑:

这是您的有效json(至少我认为是这样)

这里是csharp:

public class DemiGod
{
    public string Name { get; set; }
    public int Level { get; set; }
    public int Attack { get; set; }
    public int Dodge { get; set; }
    public int Defence { get; set; }
    public int Speed { get; set; }
}

public class PlayerGrid
{
    public int x { get; set; }
    public int y { get; set; }
}

public class AIGrid
{
    public int x { get; set; }
    public int y { get; set; }
}

public class RootObject
{
    public int PlayerID { get; set; }
    public string PlayerName { get; set; }
    public List<DemiGod> DemiGods { get; set; }
    public List<PlayerGrid> PlayerGrid { get; set; }
    public List<AIGrid> AIGrid { get; set; }
}
公共类
{
公共字符串名称{get;set;}
公共整数级别{get;set;}
公共整数攻击{get;set;}
公共int道奇{get;set;}
公共整数防御{get;set;}
公共整数速度{get;set;}
}
公共类PlayerGrid
{
公共整数x{get;set;}
公共整数y{get;set;}
}
公营飞机
{
公共整数x{get;set;}
公共整数y{get;set;}
}
公共类根对象
{
public int PlayerID{get;set;}
公共字符串播放器名称{get;set;}
公共列表{get;set;}
公共列表PlayerGrid{get;set;}
公共列表AIGrid{get;set;}
}
我不知道JsonFX是如何工作的,但我猜是这样的:

public class JsonData:MonoBehaviour{

    public RootObject root;
    void Start(){   
         root = JsonFX.Deserialize<RootObject>(jsonFile);
    }
}
公共类JsonData:MonoBehavior{
公共根对象根;
void Start(){
root=JsonFX.Deserialize(jsonFile);
}
}

因此,在我的类与我的Json不匹配之前,它将抛出Null??如果我想在我的对象中反序列化一个对象,那么怎么做呢??当然,任何线索或微小的例子都非常合适,序列化程序只能将对象转换为已知对象。您需要一个半神列表,因此需要一个对应的json。您需要考虑您的json,以便它符合您的需求。在你的情况下,c方是有道理的。重写json以使用数组,如我在回答中所示,这应该可以做到。尝试将json粘贴到json2csharp.com中,然后查看结果。与你已经拥有的相比,它不是那么直观和容易维护。是的,它起作用了。。。。我将所有对象转换为数组,并为每个对象添加了类型。现在半神构装师调用了5次,可以开始了。PlayerGrid和AIGrid构造函数调用了5次。。日志仍然返回null。。。。。修改Json并粘贴到json2sharp中。复制了c代码。。。。但它返回空值。不。。。事实上,它起作用了。我使用了错误的Json字符串来计算数据。对不起,我弄错了
public class JsonData:MonoBehaviour{

    public RootObject root;
    void Start(){   
         root = JsonFX.Deserialize<RootObject>(jsonFile);
    }
}