LitJson解析可以把类中类的字段也能解析到,但Unity自带JsonUtility却不行。以下做个测试验证:
测试类:
public class Person {
public string name;
public string address;
public Secerts secerts;
}
public class Secerts {
public string aa;
public string bb;
}
测试代码:
// Use this for initialization
void Start () {
Person pp = new Person();
pp.name = "ZhangSan";
pp.address="BeiJing";
Secerts ss = new Secerts();
ss.aa = "aa";
ss.bb = "bb";
pp.secerts = ss;
string msg1 = LitJson.JsonMapper.ToJson(pp);
string msg2 = JsonUtility.ToJson(pp);
Debug.Log(msg1);
Debug.Log(msg2);
}
运行结果;
从这里可以看出LitJson解析可以把类中类的字段也能解析到,但Unity自带JsonUtility却不行,是需要加其他附加条件吗,是的,须在类上面添加一行[System.Serializable]
public class Person {
public string name;
public string address;
public Secerts secerts;
}
[System.Serializable]
public class Secerts {
public string aa;
public string bb;
public static string cc = "cc";
}