[Unity3d]u3d请求json数据并解析

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.youkuaiyun.com/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 {     /// <summary>     /// GetPhotoList 的摘要说明     /// </summary>     public class GetPhotoList : IHttpHandler     {          public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "text/plain";             string id = context.Request.Form["id"];             string path = context.Request.PhysicalApplicationPath;             //context.Response.Write("Hello World");             List<Photo> photos = GetPhotos(id,path);             DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());             djson.WriteObject(context.Response.OutputStream, photos);         }          public List<Photo> GetPhotos(string id,string path)         {             //获取目录             string localPath = path+id + "\\";              //读取目录下的文件             if (!Directory.Exists(localPath)) return null;             string[] files = Directory.GetFiles(localPath);             List<Photo> photos = new List<Photo>();             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;     } }


==================== 迂者 丁小未 优快云博客专栏=================

MyBlog:http://blog.youkuaiyun.com/dingxiaowei2013              MyQQ:1213250243

MyTel:13262983383 

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















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366193,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值