[Unity3d]u3d请求json数据并解析
admin
2023-01-20 17:01:20
0

unity3d在跟.net进行http通信的时候,最常见的就是表单数据的提交请求了,但服务器端会返回一坨json数据,这就要求我们在unity中进行json数据的处理了,一般unity中处理json个数数据用的最多的就是LitJSON(它是.net平台下处理SON数据库的类库)。下面我就贴出源码,仅供学习参考!

关于LitJSON的安装和使用,请参考:http://www.360doc.com/content/13/0117/11/10941785_260686840.shtml

或者参考:http://blog.csdn.net/dingxiaowei2013/article/details/17115665


将LitJson.dll放在assets目录下的plugins文件下,如果没有plugins文件就手动创建一个


Client:

using UnityEngine; using System.Collections; using LitJson;  public class GetPhotoList : MonoBehaviour {      // Use this for initialization     void Start () {         StartCoroutine(GetPhotos());     }          // Update is called once per frame     IEnumerator GetPhotos(){             WWWForm    form = new WWWForm();         form.AddField("id","123");         WWW w = new WWW("http://localhost:36944/GetPhotoList.ashx",form);         while (!w.isDone){yield return new WaitForEndOfFrame();}         if (w.error != null){Debug.LogError(w.error);}         Debug.Log(w.text);                 JsonData jd = JsonMapper.ToObject(w.text);         for (int i = 0; i < jd.Count; i++)         {                         Debug.Log("id=" + jd[i]["id"]);             Debug.Log("name=" + jd[i]["name"]);         }              } }

Server:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization.Json; using System.ServiceModel; using System.ServiceModel.Web; using System.IO;  namespace UpdatePhoto {     ///      /// GetPhotoList 的摘要说明     ///      public class GetPhotoList : IHttpHandler     {          public void Proce***equest(HttpContext context)         {             context.Response.ContentType = "text/plain";             string id = context.Request.Form["id"];             string path = context.Request.PhysicalApplicationPath;             //context.Response.Write("Hello World");             List photos = GetPhotos(id,path);             DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());             djson.WriteObject(context.Response.OutputStream, photos);         }          public List GetPhotos(string id,string path)         {             //获取目录             string localPath = path+id + "\\";              //读取目录下的文件             if (!Directory.Exists(localPath)) return null;             string[] files = Directory.GetFiles(localPath);             List photos = new List();             foreach (string file in files)             {                 string filename = file.Substring(file.LastIndexOf('\\')+1);                 Photo p = new Photo();                 p.name = filename;                 p.id = id;                 photos.Add(p);             }               return photos;         }          public bool IsReusable         {             get             {                 return false;             }         }     }      public class Photo     {         public string id;         public string name;     } }


==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013              MyQQ:1213250243

MyTel:13262983383 

====================== 相互学习,共同进步 ===================


相关内容

热门资讯

一举创下3项世界纪录,中国自主... 【环球时报报道 记者 郭媛丹】21日上午,中船集团旗下中船澄西船舶修造有限公司为力拓集团建造的自主研...
试图把国内矛盾外引?专家解析菲... 【环球时报报道 记者 樊巍】21日上午,外交部边海司负责人就菲律宾在仁爱礁蓄意挑衅、袭击中国海警执法...
特朗普宣布两年后对仿制药征收1... △美国总统特朗普(资料图)当地时间7月21日,美国总统特朗普在社交媒体发文表示,自2026年8月1日...
美国贸易代表暗示将很快出台新关... 新华社纽约7月21日电(记者刘亚南) 美国贸易代表贾米森·格里尔21日在接受媒体采访时暗示,美国联邦...
中国空调“一机难求”,人民日报... 中国空调“一机难求”,创意产品持续热销,本报记者实地探访——消暑降温,欧洲民众为何追捧中国制造?(第...
美防长:对伊朗战争已耗资375... △美国国防部长赫格塞思(资料图)央视记者当地时间21日获悉,美国国防部长赫格塞思表示,美国对伊朗战争...
在全国抗议声中,泽连斯基解除乌... 据乌克兰媒体《基辅独立报》报道,乌克兰总统泽连斯基解除了乌克兰武装部队总司令瑟尔斯基的职务,并任命联...
也门胡塞武装:停靠沙特港口船只... 新华社科威特城7月21日电(记者尹炣)萨那消息:也门胡塞武装20日警告国际航运公司称,与沙特港口开展...
特朗普:胡塞武装若封锁红海,美... 新华社华盛顿7月21日电(记者黄强 徐剑梅)美国总统特朗普21日在白宫会见到访的黎巴嫩总统奥恩时表示...
AI第一城|GOAI世界人工智... 7月21日上午,GOAI世界人工智能开源大赛(Global Open-source AI Chall...