[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 

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


相关内容

热门资讯

今年上半年全国城镇新增就业69... 今天(22日)上午,人力资源社会保障部举行新闻发布会。今年上半年,全国城镇新增就业695万人,同比持...
委内瑞拉强震遇难人数升至534... 当地时间21日,委内瑞拉全国代表大会主席豪尔赫·罗德里格斯通过社交媒体通报,该国6月24日两次强震造...
高考684分花3000元咨询进... 潮新闻客户端 青年特约评论员 曾耀湘图源:视觉中国近日,据齐鲁晚报报道,河北一名2018级考生,因“...
郑州2死2伤火灾系人为纵火 郑州市公安局金水分局对外发布案件通报。通报显示,6月19日21时,郑州市金水区宏明路与柳林路东南角宏...
29岁成都小伙旅行中改道广西救... 7月14日,自发赶往广西救灾的志愿者党鑫蕊,由于劳累过度导致心功能衰竭,在贵港市覃塘区不幸离世,年仅...
清理洗衣机下水管道的方法 清理洗衣机下水管道是一项必要的任务,如果不及时清理,管道内部容易积累污垢和细菌,从而影响洗衣机的使用...
澳外长宣称:中国要就军事建设做... 7月初,中国向太平洋公海海域成功发射一枚携载训练模拟弹头的潜射战略导弹。中方重申,此为例行军事训练活...
伊朗威胁打击海水淡化厂,到底有... 据路透社报道,伊朗7月21日对科威特一座海水淡化厂发动袭击,引发海湾地区担忧,随着该地区冲突升级,更...
美新版“空军一号”刚上岗就回炉... 【环球时报综合报道】美国《防务新闻》7月20日称,美国总统特朗普透露,新版总统专机“空军一号”将被送...
全自动卧式洗衣机清理方法 随着生活水平的不断提高,全自动卧式洗衣机已经成为了家庭生活中不可或缺的一部分。它的出现方便了我们日常...