1、LitJson 库
1.1 使用说明
http://blog.csdn.net/candycat1992/article/details/10940245
1.2 下载地址
http://download.csdn.net/detail/candycat1992/6624695
http://lbv.github.io/litjson/ (官网)
1.3 导入步骤
在Assets 目录下 新建 Plugins 目录,将下载好的 LitJson.dll , 放进该目录下即可
使用时,引入 文件
<span style="white-space:pre"> </span>using LitJson;
1.4 代码示例
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class FamilyInfo {
public string name;
public int age;
public string tellphone;
public string address;
}
public class FamilyList {
public List<FamilyInfo> family_list;
}
public class LitJsonSample : MonoBehaviour {
public FamilyList m_FamilyList = null;
// Use this for initialization
void Start () {
ReloadFamilyData();
DisplayFamilyList(m_FamilyList);
}
private void ReloadFamilyData () {
// 这个例子里family的绝对路径为“Resources/family”
TextAsset s = Resources.Load("family") as TextAsset;
string tmp = s.text;
m_FamilyList = JsonMapper.ToObject<FamilyList>(tmp);
// if ( JsonMapper.HasInterpretError() ) {
// Debug.LogWarning(JsonMapper.GetInterpretError());
// }
}
private void DisplayFamilyList (FamilyList familyList) {
if ( familyList == null )
return;
foreach( FamilyInfo info in familyList.family_list ) {
Debug.Log("Name:" + info.name + " Age:" + info.age + " Tel:" + info.tellphone + " Addr:" + info.address);
}
}
// Update is called once per frame
void Update () {
}
}
2、SimpleJson 库
2.1 使用说明
http://www.omuying.com/article/152.aspx
2.2 下载地址
https://github.com/facebook-csharp-sdk/simple-json
2.3 导入步骤
2.4 代码示例
using UnityEngine;
using System.Collections;
using SimpleJson;
public class Simle : MonoBehaviour {
// Use this for initialization
void Start () {
TextAsset t = Resources.Load("1") as TextAsset;
string s = t.text;
Debug.Log(s);
analysisJson(s);
}
// Update is called once per frame
void Update () {
}
public void analysisJson(string s ){
JsonObject jsonObject = SimpleJson.SimpleJson.DeserializeObject(s) as JsonObject;
string userName = jsonObject["UserName"].ToString();
int userAge = int.Parse(jsonObject["UserAge"].ToString());
Debug.Log(userName + " = " + userAge);
JsonArray itemList = jsonObject["ItemList"] as JsonArray;
foreach(JsonObject itemObject in itemList)
{
string itemName = itemObject["ItemName"].ToString();
int itemNum = int.Parse(itemObject["ItemNum"].ToString());
Debug.Log(itemName + " = " + itemNum);
}
}
}