一、最终解析结果
二、Json数据文件
三、一些坑
-
Json保存的格式Unicode(UTF-8,无签名),如果有签名会报错 未测试
-
解析类中的字段必须和Json中的字段相对应
3. 如果想解析多条数据,改变Json字符串,先将他装在一个字段中,然后在解析
4. 解析类必须加[System.Serializable]
四、源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using
System.Collections.Generic;
using
UnityEngine;
public
class
JsonSerializerDemo : MonoBehaviour {
void
Start () {
//AssetBundle ab = AssetBundle.LoadFromFile(Application.dataPath+ "/Resources/guidesteplist.ab");
TextAsset ta=Resources.Load<TextAsset>(
"GuideStepList"
);
if
(ta !=
null
)
{
//TextAsset ta = ab.LoadAsset<TextAsset>("guidesteplist");
string
str = ta.text;
if
(!
string
.IsNullOrEmpty(str))
{
ExampleList gstd = JsonUtility.FromJson<ExampleList>(str);
for
(
int
i = 0; i < gstd.tempList.Count; i++)
{
Debug.LogError(gstd.tempList[i].ToString());
}
}
}
else
{
Debug.LogError(
"加载不到资源"
);
}
}
}
[System.Serializable]
public
class
ExampleList
{
public
List<GuideStepData> tempList;
}
[System.Serializable]
public
class
GuideStepData
{
public
override
string
ToString()
{
return
"Data:[ID:"
+ ID +
",Type:"
+ Type +
"]"
;
}
public
int
ID;
public
string
Type;
}
|