解析嵌套的Json [英] c# - Unity parse nested Json
本文介绍了解析嵌套的Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:我试图使用Unity JsonUtility解析嵌套的JSON,但是如果我记录了其中一个嵌套参数,则结果为Null.
I have the following problem: I'm trying to parse a nested JSON using the Unity JsonUtility, but if i'm logging one of the nested parameters i get Null as a result.
这是杰森(Json):
Here's the Json:
{
"basic": {
"name": "Demo Bow"
},
"effect": {
"damage": {
"stages": 3,
"one": 4,
"two": 10,
"three": 40
}
}
}
这是我的代码:
public class Basic
{
public string name;
}
public class Damage
{
public int stages;
public int one;
public int two;
public int three;
}
public class Effect
{
public Damage damage;
}
public class RootObject
{
public Basic basic;
public Effect effect;
}
不能重复,因为我已经删除了"{get; set;}" 还有遗失的代码.
No its not a duplicate, cause I already removed the "{ get; set; }" And heres the missing code.
public static RootObject CreateFromJSON(string json){
RootObject weapon = JsonUtility.FromJson <RootObject> (json);
return weapon;
}
感谢您的帮助
推荐答案
您缺少类的[System.Serializable]
属性,JsonUtility
需要能够对您的类进行序列化或反序列化.
You are missing the [System.Serializable]
attribute on your classes, it is needed for JsonUtility
to be able to serialize or deserialize your classes.
[System.Serializable]
public class Basic
{
public string name;
}
[System.Serializable]
public class Damage
{
public int stages;
public int one;
public int two;
public int three;
}
[System.Serializable]
public class Effect
{
public Damage damage;
}
[System.Serializable]
public class RootObject
{
public Basic basic;
public Effect effect;
}
这篇关于解析嵌套的Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文