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
 {   
          for(int j=0;j
         {
                for(int k=0;k
                {
                    print(data[1][0][0]["SenceInfo"]["Pos"]);
                    JsonData dataInfo=data[i][j][k];
                }
         }
}

你可能感兴趣的:(Unity3D)