位置: 编程技术 - 正文
推荐整理分享unity请求json数据并解析(unity jsonobject),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity jsonobject,unity post请求,unity中json有几种结构,unity jsonutility,unity中json有几种结构,unity jsonobject,unity解析json数据,unity json,内容如对您有帮助,希望把文章链接给更多的朋友!
unity3d在跟.net进行http通信的时候,最常见的就是表单数据的提交请求了,但服务器端会返回一坨json数据,这就要求我们在unity中进行json数据的处理了,一般unity中处理json个数数据用的最多的就是LitJSON(它是.net平台下处理SON数据库的类库)。下面我就贴出源码,仅供学习参考!
关于LitJSON的安装和使用,请参考: view plaincopyprint?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",""); WWW w = new WWW(" 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:
[plain] view plaincopyprint?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 = pathid "\"; //读取目录下的文件 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; } }Unity3D中销毁对象的方式 转载自:
Unity3D获取对象的5中方式 转载自:
封装 UnityEngine.Debug.Log 为Dll ,游戏发布关闭 Log 减少性能消耗 本文参考雨松Mono的文章:
标签: unity jsonobject
本文链接地址:https://www.jiuchutong.com/biancheng/380126.html 转载请保留说明!上一篇:Unity3d 导入图片 自动修改Texture Type为Sprite (2D and UI) 及设置 Packing Tag为文件夹名(unity怎么导入3d模型)
下一篇:Unity3D中销毁对象的方式(unity销毁预制体)
友情链接: 武汉网站建设