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 ?
  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 ?
  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. /// <summary>
  12. /// GetPhotoList 的摘要说明
  13. /// </summary>
  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<Photo> photos = GetPhotos(id,path);
  23. DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());
  24. djson.WriteObject(context.Response.OutputStream, photos);
  25. }
  26. public List<Photo> 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<Photo> photos = new List<Photo>();
  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. }

unity请求json数据并解析相关推荐

  1. C#获取http请求的JSON数据并解析

    文章目录 1️⃣ C#获取http请求的JSON数据并解析 1.1 获取http的JSON数据 1.2 解析JSON数据 优质资源分享 C#获取http请求的JSON数据并解析,第一步,先拿到http ...

  2. 利用百度APIStoreSDK获取Json数据并解析加载到ListView上

    效果图(这里只是截取了List中的一项): 一.:登录百度ApiStore,获取移动sdk Android版(同时有使用文档和示例),将jar包导入到项目中 Android Studio请在放入lib ...

  3. C++通过HTTP请求Get或Post方式请求Json数据

    转载:C++通过HTTP请求Get或Post方式请求Json数据 最近在工作中,由于合作商只提供uRL,我这边需要通过HTTP请求Get或Post方式请求Json数据,然后解析JSON格式,解析jso ...

  4. 浏览器禁止跨域请求json数据解决方法--jsonp

    浏览器禁止跨域请求json数据解决方法--jsonp 参考文章: (1)浏览器禁止跨域请求json数据解决方法--jsonp (2)https://www.cnblogs.com/uyisi/p/56 ...

  5. jquery的ajax,请求JSON数据。

    jquery的ajax,请求JSON数据. 第一个页面:1.htm <!DOCTYPE html> <html> <head> <title></ ...

  6. c语言json映射,GitHub - xujun621/cson: 基于C语言的json数据映射解析库

    CSON 基于cJSON,运行于C语言平台的json-struct模型解析工具 简介 CSON是一个简单的cJSON的二次封装,相比于使用原生cJSON一层一层解析的方式,CSON采用模型映射的方式, ...

  7. android json.out,Android 之 json数据的解析(jsonReader)

    json数据的解析相对而言,还是比较容易的,实现的代码也十分简单.这里用的是jsonReade方法来进行json数据解析. 1.在解析之前,大家需要知道什么是json数据. json数据存储的对象是无 ...

  8. swift php json解析,Swift 4.0 | JSON数据的解析和编码

    文 / 菲拉兔 自己撸的图 要求: Platform: iOS8.0+ Language: Swift4.0 Editor: Xcode9 [问题补充2017-09-28] 最近我发现了一个问题:在S ...

  9. C# 后台发送Post Get请求 Json数据或表单数据

    Http请求类 public class HttpResponse{#region Static Fieldprivate static readonly string DefaultUserAgen ...

最新文章

  1. IOS开发入门环境搭建输出helloworld
  2. selenium自动化测试框架_selenium自动化测试框架之PO设计模式
  3. Redis工作笔记-Set类型
  4. 2019级软件1班安卓实训总结
  5. html表单中动态添加下拉框,antd Select下拉菜单动态添加option里的内容操作
  6. django框架搭建网页后台,运行后网页打不开的解决方法--windows系统下
  7. maven命令-P 参数引发的思考
  8. 【maya】模型学习
  9. Select at least one project的解决方法
  10. canvas设置lineWidth属性,出现线条被fill覆盖问题。
  11. python爬虫学习爬取股票数据
  12. Unity3D 运用GL屏幕画图(阴阳师画符)
  13. 福利,架构师之路定制T恤
  14. python量化交易策略实例_实践《Python与量化投资从基础到实战》PDF代码+《量化交易之路用Python做股票量化分析》PDF代码解释...
  15. 三,标识符(identifier)讲解
  16. 人的感性是否也属于理性的一种
  17. 面试时,问你有什么缺点,到底该怎么回答?我总结了“3个3”
  18. AI算法实现CSGO自动锁头辅助脚本
  19. C语言学生信息管理系统
  20. adb 输入回车命令_Android超级终端(Adb Shell)常用命令、命令大全-持续更新 | 何连超的博客小站...

热门文章

  1. 《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
  2. 关于python说法正确的有哪些-以下关于Python的说法中正确的是哪一项?
  3. 使用福昕PDF编辑器对PDF文档进行电子签名
  4. Spring Batch 批量处理策略 1
  5. carsim与simulink联合仿真 动驱动 两轮独立驱动电动汽车控制策略。 分为低速和高速两种策略优化分配驱动力矩
  6. 高等数学:第一章 函数与极限(4)无穷小与无穷大
  7. 职场--电挂支持之注意事项
  8. js实现table表头冻结(scroll时固定)
  9. gensim实现LDA(Latent Dirichlet Allocation)算法提取主题词(topic)
  10. 订餐网站服务器配置,网上订餐模块介绍