0%

Unity - JsonUtility

JsonUtility 须知

  • JsonUtility 效率高
  • JsonUtility 无法直接的对 List 对象进行序列化和反序列化,需要将 List 包装(Wrap)到一个类当中,然后序列化和反序列化包含这个List 的对象即可。
  • JsonUtility 不支持 Dictionary。官方的文档中有说明
  • 我们要做的就是在JsonUtility局限性或者说是“规则”之下去使用。比如 Dictionary 可以使用数组来替代,一定要使用 Dictionary,则可以进行二次转换。

JsonUtility 示例

下面是两个在所有示例中通用的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Goods.cs
[System.Serializable]
public class Goods {
public int ID;
public string Name;
public string Introduction;

public Goods(int id, string name, string introduction) {
ID = id;
Name = name;
Introduction = introduction;
}
}

// GoodsList.cs
using System.Collections.Generic;

[System.Serializable]
public class GoodsList {
public List<Goods> goodsList;
}

示例一 序列化一个对象

本示例将一个 Goods 类的对象 goods 序列化成 JSON 字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
// JSONTest.cs
using UnityEngine;

public class JSONTest : MonoBehaviour {

4void Start () {
Goods goods = new new Goods(1, "Hat", "A nice hat.");

// 序列化
string json = JsonUtility.ToJson(goods);
Debug.Log(json);
4}
}
1
2
# 输出
{"ID":1,"Name":"Hat","Introduction":"A nice hat."}

示例二 序列化一个链表

本示例将 GoodsList 序列化成 JSON 字符串,其中 GoodsList 包含一个有着三个 Goods 对象的链表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// JSONTest.cs
using System.Collections.Generic;
using UnityEngine;

public class JSONTest : MonoBehaviour {

void Start () {
GoodsList goodsList = new GoodsList();
goodsList.goodsList = new List<Goods>() {
new Goods(1, "Hat", "A nice hat."),
new Goods(2, "Gloves", "A Warm Gloves."),
new Goods(3, "Shoes", "Tattered Shoes."),
};

// 序列化
string json = JsonUtility.ToJson(goodsList);
Debug.Log(json);
}
}
1
2
# 输出
{"goodsList":[{"ID":1,"Name":"Hat","Introduction":"A nice hat."},{"ID":2,"Name":"Gloves","Introduction":"A Warm Gloves."},{"ID":3,"Name":"Shoes","Introduction":"Tattered Shoes."}]}

示例三 将 JSON 字符串反序列化为类对象

本示例中将 JSON 字符串 json 反序列化成 Goods 类对象 result,并访问对象成员。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections.Generic;
using UnityEngine;

public class JSONTest : MonoBehaviour {

4void Start () {

Goods goods = new Goods(1, "Hat", "A nice hat.");

// 序列化
string json = JsonUtility.ToJson(goods);

// 反序列化
Goods result = JsonUtility.FromJson<Goods>(json);
Debug.Log("ID:"+result.ID+", Name:"+result.Name+", Introduction:"+result.Introduction);
4}
}
1
2
# 输出
ID:1, Name:Hat, Introduction:A nice hat.

示例四 将 JSON 字符串反序列化为类对象的链表

本示例中,读取 Resources 文件夹中 JSON文件 GoodsList.json,将其反序列化为一个带有一个链表的类 GoodsList,并访问其链表的元素。

GoodsList.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"goodsList": [
{
"ID": 1,
"Name": "Hat",
"Introduction": "A nice hat."
},
{
"ID": 2,
"Name": "Gloves",
"Introduction": "A Warm Gloves."
},
{
"ID": 3,
"Name": "Shoes",
"Introduction": "Tattered Shoes."
}
]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// JSONTest.cs
using UnityEngine;

public class JSONTest : MonoBehaviour {

void Start() {

TextAsset textAsset = Resources.Load<TextAsset>("GoodsList");
string json = textAsset.text;

GoodsList goodslist = JsonUtility.FromJson<GoodsList>(json);

foreach (Goods item in goodslist.goodsList) {
Debug.Log("ID:" + item.ID + ", Name:" + item.Name + ", Introduction:" + item.Introduction);
}
}
}
1
2
3
4
# 输出
ID:1, Name:Hat, Introduction:A nice hat.
ID:2, Name:Gloves, Introduction:A Warm Gloves.
ID:3, Name:Shoes, Introduction:Tattered Shoes.