资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

[Unity3d]u3d请求json数据并解析-创新互联

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

创新互联主要从事成都网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务宁蒗,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

关于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

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


另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:[Unity3d]u3d请求json数据并解析-创新互联
本文网址:http://cdkjz.cn/article/jhjej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220