Unity3d Json解析库


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);
		}
	}

}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值