Unity 使用 Newtonsoft json 库进行序列化和反序列化

Newtonsoft Json.NET是.NET生态下标准json解析库,但是不支持 Unity 引擎。但 GitHub 上有一个可以支持 Unity 的库Newtonsoft Json.NET for Unity3D。本文来介绍一下在 Unity 使用 Newtonsoft 库进行 json 解析。支持序列化字典类型。Unity 自带的 JsonUtility 不支持字典类型。

在https://github.com/SaladLab/Json.Net.Unity3D/releases下载 unitypackage 导入到 Unity 工程里面。

  1. 对象序列化为 json 字符串
//先定义一个需要序列化的People类
[Serializable]
public class People
{
    public string name;
    public int age;
    public DateTime birthday;
    public bool isMarried;
    public float money;
}

//序列化 Serialize
public void SerializeJson()
{
    People zhangsan = new People();
    zhangsan.name = "张三";
    zhangsan.age = 20;
    zhangsan.birthday = DateTime.Now.AddYears(-20);
    zhangsan.isMarried = false;
    zhangsan.money = 10.0f;

    string jsonStr = JsonConvert.SerializeObject(zhangsan);
    Debug.Log("zhangsan json: " + jsonStr);
    //output: zhangsan json: {"name":"张三","age":20,"birthday":"1999-03-31T17:15:29.489553+08:00","isMarried":false,"money":10.0}
}
  1. json 字符串反序列化为对象
//反序列化 Deserialize
public void DeserializeJson()
{
    string jsonStr = "{\"name\" :\"张三\",\"age\":30,\"birthday\":\"1990-03-31T17:15:29\",\"isMarried\":true,\"money\":10.0}";

    People zhangsan = JsonConvert.DeserializeObject<People>(jsonStr);
    Debug.Log("zhangsan.name " + zhangsan.name + " birthday: " + zhangsan.birthday + " isMarried: " + zhangsan.isMarried);

    //output: zhangsan.name 张三 birthday: 03/31/1990 17:15:29 isMarried: True
}
  1. 序列化对象数组
public void SerializeArray()
{
    People[] array = new People[4];
    for (int i = 0; i < array.Length; i++)
    {
        People people = new People();
        people.name = "People" + i.ToString();
        people.age = 18 + i;
        people.birthday = DateTime.Now.AddYears(-18-i);
        people.isMarried = true;
        people.money = 1000 * i;
        array[i] = people;
    }

    string strs = JsonConvert.SerializeObject(array);
    Debug.Log("array: " + strs);
    //output: array: [{"name":"People0","age":18,"birthday":"2001-03-31T18:02:35.038662+08:00","isMarried":true,"money":0.0},
    //{"name":"People1","age":19,"birthday":"2000-03-31T18:02:35.039509+08:00","isMarried":true,"money":1000.0},
    //{"name":"People2","age":20,"birthday":"1999-03-31T18:02:35.039512+08:00","isMarried":true,"money":2000.0},
    //{"name":"People3","age":21,"birthday":"1998-03-31T18:02:35.039513+08:00","isMarried":true,"money":3000.0}]
}
  1. 反序列化对象数组
public void DeserializeArray()
{
    JArray jArray = JArray.Parse("[{\"name\":\"People0\",\"age\":18,\"birthday\":\"2001-03-31T18:02:35.038662\",\"isMarried\":true,\"money\":0.0},{\"name\":\"People1\",\"age\":19,\"birthday\":\"2000-03-31T18:02:35.039509\",\"isMarried\":true,\"money\":1000.0},{\"name\":\"People2\",\"age\":20,\"birthday\":\"1999-03-31T18:02:35.039512\",\"isMarried\":true,\"money\":2000.0}]");
    Debug.Log(jArray.Count);
    foreach (var item in jArray)
    {
        Debug.Log("item path: " + item.Path + " string: " + item.ToString());
    }
    //outpus:
    //3
    //item path: [0] string: {
    //"name": "People0",
    //  "age": 18,
    //  "birthday": "2001-03-31T18:02:35.038662",
    //  "isMarried": true,
    //  "money": 0.0
    //}
    //item path: [1] string: {
    //  "name": "People1",
    //  "age": 19,
    //  "birthday": "2000-03-31T18:02:35.039509",
    //  "isMarried": true,
    //  "money": 1000.0
    //}
    //item path: [2] string: {
    //  "name": "People2",
    //  "age": 20,
    //  "birthday": "1999-03-31T18:02:35.039512",
    //  "isMarried": true,
    //  "money": 2000.0
    //}

    People[] peoples = jArray.ToObject<People[]>();
    for (int i = 0; i < peoples.Length; i++)
    {
        Debug.Log("peoples " + i.ToString() + " " + peoples[i].name + " " + peoples[i].birthday + " " + peoples[i].money);
    }
    //output:
    //peoples 0 People0 03/31/2001 18:02:35 0
    //peoples 1 People1 03/31/2000 18:02:35 1000
    //peoples 2 People2 03/31/1999 18:02:35 2000
}

参考资料:

上一篇:[Python] Manipulate Data with Dictionaries in Python


下一篇:IdentityServer4 中文文档 -4- (简介)打包和构建