[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.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 {     /// <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;     } }


==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013              MyQQ:1213250243

MyTel:13262983383 

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















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366193,如需转载请自行联系原作者
相关文章
|
29天前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
20 0
|
25天前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
255 0
|
7天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
24 0
|
11天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
11 2
|
25天前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
79 2
|
25天前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
44 0
|
29天前
|
JSON 数据格式
糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
19 1
|
3月前
|
JSON PHP 数据格式
|
3月前
|
JSON JavaScript 前端开发
JavaScript 如何对 JSON 数据进行冒泡排序?
JavaScript 如何对 JSON 数据进行冒泡排序?
51 0
|
1月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%

推荐镜像

更多