unity请求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:
![](https://code.csdn.net/assets/CODE_ico.png)
- 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:
![](https://code.csdn.net/assets/CODE_ico.png)
- 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;
- }
- }
unity请求json数据并解析相关推荐
- C#获取http请求的JSON数据并解析
文章目录 1️⃣ C#获取http请求的JSON数据并解析 1.1 获取http的JSON数据 1.2 解析JSON数据 优质资源分享 C#获取http请求的JSON数据并解析,第一步,先拿到http ...
- 利用百度APIStoreSDK获取Json数据并解析加载到ListView上
效果图(这里只是截取了List中的一项): 一.:登录百度ApiStore,获取移动sdk Android版(同时有使用文档和示例),将jar包导入到项目中 Android Studio请在放入lib ...
- C++通过HTTP请求Get或Post方式请求Json数据
转载:C++通过HTTP请求Get或Post方式请求Json数据 最近在工作中,由于合作商只提供uRL,我这边需要通过HTTP请求Get或Post方式请求Json数据,然后解析JSON格式,解析jso ...
- 浏览器禁止跨域请求json数据解决方法--jsonp
浏览器禁止跨域请求json数据解决方法--jsonp 参考文章: (1)浏览器禁止跨域请求json数据解决方法--jsonp (2)https://www.cnblogs.com/uyisi/p/56 ...
- jquery的ajax,请求JSON数据。
jquery的ajax,请求JSON数据. 第一个页面:1.htm <!DOCTYPE html> <html> <head> <title></ ...
- c语言json映射,GitHub - xujun621/cson: 基于C语言的json数据映射解析库
CSON 基于cJSON,运行于C语言平台的json-struct模型解析工具 简介 CSON是一个简单的cJSON的二次封装,相比于使用原生cJSON一层一层解析的方式,CSON采用模型映射的方式, ...
- android json.out,Android 之 json数据的解析(jsonReader)
json数据的解析相对而言,还是比较容易的,实现的代码也十分简单.这里用的是jsonReade方法来进行json数据解析. 1.在解析之前,大家需要知道什么是json数据. json数据存储的对象是无 ...
- swift php json解析,Swift 4.0 | JSON数据的解析和编码
文 / 菲拉兔 自己撸的图 要求: Platform: iOS8.0+ Language: Swift4.0 Editor: Xcode9 [问题补充2017-09-28] 最近我发现了一个问题:在S ...
- C# 后台发送Post Get请求 Json数据或表单数据
Http请求类 public class HttpResponse{#region Static Fieldprivate static readonly string DefaultUserAgen ...
最新文章
- IOS开发入门环境搭建输出helloworld
- selenium自动化测试框架_selenium自动化测试框架之PO设计模式
- Redis工作笔记-Set类型
- 2019级软件1班安卓实训总结
- html表单中动态添加下拉框,antd Select下拉菜单动态添加option里的内容操作
- django框架搭建网页后台,运行后网页打不开的解决方法--windows系统下
- maven命令-P 参数引发的思考
- 【maya】模型学习
- Select at least one project的解决方法
- canvas设置lineWidth属性,出现线条被fill覆盖问题。
- python爬虫学习爬取股票数据
- Unity3D 运用GL屏幕画图(阴阳师画符)
- 福利,架构师之路定制T恤
- python量化交易策略实例_实践《Python与量化投资从基础到实战》PDF代码+《量化交易之路用Python做股票量化分析》PDF代码解释...
- 三,标识符(identifier)讲解
- 人的感性是否也属于理性的一种
- 面试时,问你有什么缺点,到底该怎么回答?我总结了“3个3”
- AI算法实现CSGO自动锁头辅助脚本
- C语言学生信息管理系统
- adb 输入回车命令_Android超级终端(Adb Shell)常用命令、命令大全-持续更新 | 何连超的博客小站...
热门文章
- 《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
- 关于python说法正确的有哪些-以下关于Python的说法中正确的是哪一项?
- 使用福昕PDF编辑器对PDF文档进行电子签名
- Spring Batch 批量处理策略 1
- carsim与simulink联合仿真 动驱动 两轮独立驱动电动汽车控制策略。 分为低速和高速两种策略优化分配驱动力矩
- 高等数学:第一章 函数与极限(4)无穷小与无穷大
- 职场--电挂支持之注意事项
- js实现table表头冻结(scroll时固定)
- gensim实现LDA(Latent Dirichlet Allocation)算法提取主题词(topic)
- 订餐网站服务器配置,网上订餐模块介绍