unity3d 使用LitJson解析 JSON格式数据

UnityLitJson下载
服务器端格式:
[{"people":[
{"name":"fff","pass":"123456","age":"1", "info":{"sex":"man"}}, 
{"name":"god","pass":"123456","age":"1","info":{"sex":"woman"}}, 
{"name":"kwok","pass":"123456","age":"1","info":{"sex":"man"}},
{"name":"tom","pass":"123456","age":"1","info":{"sex":"woman"}}
]}
]

客户端
using UnityEngine;
using System.Collections;
using LitJson;

public class LoadControl_c:MonoBehaviour 
{
private GameObject plane;

public string url = "http://127.0.0.1/test2.txt";

// Use this for initialization
void Start()
{
StartCoroutine(LoadTextFromUrl());

//StartCoroutine(DoSomething());

//Book book = new Book("Android dep");

//InvokeRepeating("LaunchProjectile", 1, 5);
}



IEnumerator DoSomething() 
{
yield return new WaitForSeconds(3);
}

IEnumerator LoadTextFromUrl()
{
if (url.Length > 0)
{
WWW www = new WWW(url);
yield return www;
//string data = www.data.ToString().Substring(1); 
string data = www.text.ToString().Substring(1); 

// 下面是关键
print(data);

LitJson.JsonData jarr = LitJson.JsonMapper.ToObject(www.text);

if(jarr.IsArray)
{

for (int i = 0; i < jarr.Count; i++)
{
Debug.Log(jarr[i]["people"]);

JsonData jd = jarr[i]["people"];

for(int j = 0; j < jd.Count; j++)
{
Debug.Log(jd[j]["name"]);
}
}
}
}
}

}


litjson的其他嵌套应用

JsonData data=JsonMapper.ToObject(jsonData);   

 for(int i=0;i<data.Count;i++)
 {   
          for(int j=0;j<data[i].Count;j++)
         {
                for(int k=0;k<data[i][j].Count;k++)
                {
                    print(data[1][0][0]["SenceInfo"]["Pos"]);
                    JsonData dataInfo=data[i][j][k];
                }
         }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值