-
Unity3D脚本:Unity3D使用LitJson解析服务器上的JSON...
-
请保证工程里面已经加入了LitJson.dll服务器上JSON的内容。
[{"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"}}
]}
]
LoadControl_c代码:
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 = ().Substring(1);
string data = ().Substring(1);
// 下面是关键
print(data);
LitJson.JsonData jarr = LitJson.JsonMapper.ToObject();
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"]);
}
}
}
}
}
}
推荐文章