Unity之Json解析Litjson和自带JsonUtility解析区别

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";
}
—— 完 ——
相关推荐
评论

立 为 非 似

中 谁 昨 此

宵 风 夜 星

。 露 , 辰

文章点击榜

细 无 轻 自

如 边 似 在

愁 丝 梦 飞

。 雨 , 花