unity用json和服务器数据交互

第一种类型:服务器json数据是个对象

/// <summary>
/// 获取用户信息初始化信息
/// </summary>
void InitUserMessage()
{
//服务器ip端口和传入参数
string path = string.Format("{0}/GetUserInfo?UserId={1}", PublicJs.url_RequestPrefix, PublicJs.GUID);
StartCoroutine(GetUserInfo(path));
}
/// <summary>
/// 从服务器获取数据
/// </summary>
/// <param name="urlParh">服务器ip和端口</param>
/// <returns></returns>
IEnumerator GetUserInfo(string urlParh)
{
Debug.Log(urlParh); DownloadItem down = new DownloadItem(urlParh);
yield return down.www; string wwwText = down.Text();
Debug.Log(wwwText);
if (string.IsNullOrEmpty(wwwText) == false)
{
var curUser = Json.Deserialize(wwwText) as Dictionary<string, object>;
view.InitUserMessage(curUser["UserName"].ToString(), curUser["ClassName"].ToString(), curUser["Coins"].ToString());
}
else
{
Debug.Log("信息错误");
}
}

第二种类型:服务器数据是对象数组

IEnumerator RefreshUIData()
{
string urlPath = string.Format("{0}/GetTrends?", PublicJs.url_RequestPrefix);
DownloadItem down = new DownloadItem(urlPath);
yield return down.www;
string wwwText = down.Text();
Debug.Log(urlPath);
Debug.Log(wwwText);
List<object> listObject = Json.Deserialize(wwwText) as List<object>; foreach (var i in listObject)
{
Dictionary<string, object> row = i as Dictionary<string, object>;
HYDTAts hydt = new HYDTAts();
hydt.ID = row["ID"].ToString();
hydt.Name = row["Title"].ToString();
hydtList.Add(hydt);
} //hydtList = CSVFileHelp.GetCsvHYDT();
//Debug.Log(hydtList.Count); int tp = 0;
ItemHYDT itemHYDT;
GameObject obj;
foreach (HYDTAts item in hydtList)
{
obj = NGUITools.AddChild(view.GridObj, view.ItemObj);
obj.name = tp.ToString("D3");
obj.AddComponent<UIDragScrollView>();
obj.transform.localPosition = new Vector3(0, -50 * tp, 0);
itemHYDT = obj.AddComponent<ItemHYDT>();
itemHYDT.SetLabelText(item.Name);
itemHYDT.BindValue(item);
itemHYDT.OnClick += ItemHYDT_OnClick;
tp++;
} yield return 0;
}
上一篇:Swift 循环、数组 字典的遍历


下一篇:python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。