unity请求json数据并解析

桃扇骨 2022-08-08 06:18 371阅读 0赞

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:

[csharp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片

  1. using UnityEngine;
  2. using System.Collections;
  3. using LitJson;
  4. public**class** GetPhotoList : MonoBehaviour {
  5. // Use this for initialization
  6. void Start () {
  7. StartCoroutine(GetPhotos());
  8. }
  9. // Update is called once per frame
  10. IEnumerator GetPhotos(){
  11. WWWForm form = new WWWForm();
  12. form.AddField(“id”,”123”);
  13. WWW w = new WWW(“http://localhost:36944/GetPhotoList.ashx",form);
  14. while (!w.isDone){yield return**new** WaitForEndOfFrame();}
  15. if (w.error != null){Debug.LogError(w.error);}
  16. Debug.Log(w.text);
  17. JsonData jd = JsonMapper.ToObject(w.text);
  18. for (int i = 0; i < jd.Count; i++)
  19. {
  20. Debug.Log(“id=” + jd[i][“id”]);
  21. Debug.Log(“name=” + jd[i][“name”]);
  22. }
  23. }
  24. }

Server:

[plain] view plain copy print ? 在CODE上查看代码片 派生到我的代码片

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Runtime.Serialization.Json;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Web;
  8. using System.IO;
  9. namespace UpdatePhoto
  10. {
  11. ///
  12. /// GetPhotoList 的摘要说明
  13. ///
  14. public class GetPhotoList : IHttpHandler
  15. {
  16. public void ProcessRequest(HttpContext context)
  17. {
  18. context.Response.ContentType = “text/plain”;
  19. string id = context.Request.Form[“id”];
  20. string path = context.Request.PhysicalApplicationPath;
  21. //context.Response.Write(“Hello World”);
  22. List photos = GetPhotos(id,path);
  23. DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());
  24. djson.WriteObject(context.Response.OutputStream, photos);
  25. }
  26. public List GetPhotos(string id,string path)
  27. {
  28. //获取目录
  29. string localPath = path+id + “\\“;
  30. //读取目录下的文件
  31. if (!Directory.Exists(localPath)) return null;
  32. string[] files = Directory.GetFiles(localPath);
  33. List photos = new List();
  34. foreach (string file in files)
  35. {
  36. string filename = file.Substring(file.LastIndexOf(‘\\‘)+1);
  37. Photo p = new Photo();
  38. p.name = filename;
  39. p.id = id;
  40. photos.Add(p);
  41. }
  42. return photos;
  43. }
  44. public bool IsReusable
  45. {
  46. get
  47. {
  48. return false;
  49. }
  50. }
  51. }
  52. public class Photo
  53. {
  54. public string id;
  55. public string name;
  56. }
  57. }

发表评论

表情:
评论列表 (有 0 条评论,371人围观)

还没有评论,来说两句吧...

相关阅读

    相关 unity请求json数据

    unity3d在跟.net进行http通信的时候,最常见的就是表单数据的提交请求了,但服务器端会返回一坨json数据,这就要求我们在unity中进行json数据的处理了,一般u

    相关 Json数据

    1、JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方