
在本文中将介绍如何在 Unity3D 中解析 JSON 数据,3D天堂将会使用 SimpleJson 库来解析本文中的 JSON 数据。
教程将使用下面提到的 JSON 数据进行加载和解析:
{
"Department":
[{"ID":"1","Name":"Electrical","Discription":"Electrical Engineering","Students":[{"id":"1","name":"John"},{"id":"2","name":"Mary"}]},
{"ID":"2","Name":"Civil","Discription":"Civil Engineering","Students":[{"id":"1","name":"Jack"},{"id":"2","name":"Helen"}]}]
}
可以通过以下访问上述 JSON 数据:
{"Department":[{"ID":"1","Name":"Electrical","Discription":"Electrical Engineering","Students":[{"id":"1","name":"John"},{"id":"2","name":"Mary"}]},
{"ID":"2","Name":"Civil","Discription":"Civil Engineering","Students":[{"id":"1","name":"Jack"},{"id":"2","name":"Helen"}]}]}
在上面提到的 JSON 示例中,为 Department 节点添加了 2 个条目。
但在此示例中,将使用 SimpleJSON 库检索 JSON 数据中关联的所有值,可以在此处查看 SimpleJSON 库。
- 将 SimpleJSON.cs 文件复制到 Unity 项目的插件文件夹中
- 在脚本文件中添加 SimpleJSON 命名空间。
- 把 SharpZipLib.dll 也复制到 Plugin 文件夹里,除非想使用压缩功能,否则忽略它。
从上面提到的 URL 存储 JSON,并将其存储在字符串类型变量中。
// implememt WWW to get json data from any url
string url = "https://su.i3dtt.com/Department.txt";
WWW www = new WWW (url);
yield return www;
// store text in www to json string
if (string.IsNullOrEmpty(www.error)) {
// store www.text in a string type variable
jsonData = www.text;
}
解析 JSON 字符串并检索其中的值:
// use simpleJSON to get values stored in JSON data for different key value pair
JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);
// get values at the nodes, to get values at node either use the name directly or the position number of the node
// here instead of "Department", you can also write jsonNode[0][0]
Debug.Log ("Department 0 "+ jsonNode["Department"][0].ToString());
Debug.Log ("Department 1 " + jsonNode["Department"][1].ToString());
可以使用节点名称或它在 JSON 中的位置,以下两行代码将给出相同的结果。
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][0].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0]["ID"].ToString());
如下所示的是此示例的完整 C# 脚本:
using UnityEngine;
using System.Collections;
using SimpleJSON;
public class ParseUsingSimpleJson : MonoBehaviour {
string jsonData;
// Use this for initialization
IEnumerator Start () {
// implememt WWW to get json data from any url
string url = "https://su.i3dtt.com/Department.txt";
WWW www = new WWW (url);
yield return www;
// store text in www to json string
if (string.IsNullOrEmpty(www.error)) {
jsonData = www.text;
}
// use simpleJSON to get values stored in JSON data for different key value pair
JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);
// get values at the nodes, to get values at node either use the name directly or the position number of the node
// here instead of "Department", you can also write jsonNode[0][0]
Debug.Log ("Department 0 "+ jsonNode["Department"][0].ToString());
Debug.Log ("Department 1 " + jsonNode["Department"][1].ToString());
// get individual values from Department 0
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0]["ID"].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][1].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][2].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][3][0][0].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][3][0][1].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][3][1][0].ToString());
Debug.Log ("JsonNode " + 0 + " " + jsonNode["Department"][0][3][1][1].ToString());
}
}
输出结果:

…
以上是3D天堂关于在Unity中解析JSON数据的全部内容,如果你有任何反馈,请随时在本页面下方留言。