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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hemiaoyuan1989/article/details/92635721
首先先写几个测试类:

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”;
}
再次运行:解析结果一致。

public class Person {
public static string A=“A”;
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”;
}
运行结果。

可以看出,类里的字段有static修饰,自带类解析不会解析到。Litjson是可以的。
————————————————
版权声明:本文为CSDN博主「hemy1989」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hemiaoyuan1989/article/details/92635721

你可能感兴趣的:(Unity)