您好,登錄后才能下訂單哦!
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"]); } } }
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 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<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; } }
==================== 迂者 丁小未 CSDN博客專欄=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
MyTel:13262983383
====================== 相互學習,共同進步 ===================
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。