unity请求json数据并解析
2015-05-08 00:15
489 查看
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
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","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:
[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 = 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;
}
}
关于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
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","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:
[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 = 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数据并解析
- android通过httpClient请求获取JSON数据并且解析
- Android---Volley请求 Json解析 xUtils数据存储
- URL请求并解析JSON数据
- AsyncHttpClient 网络请求+ fastJson解析数据
- SpringMVC解析JSON请求数据问题解析
- Unity 使用JsonFx解析本地json数据
- android通过httpClient请求获取JSON数据并且解析
- 使用Unity自带的解析工具完成Json数据的解析
- JAVA学习笔记--线程请求Json数据,并解析绑定到listView
- Android网络请求与JSON解析和数据抓取小实例
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- Extjs中向数据字典发送请求及解析json数据
- IOS请求php返回的数据JSON数据无法正确解析 utf-8格式
- Unity之JSON数据解析
- struts2+jquery+Ajax异步请求发送与解析json数据
- Android--网络请求与json数据解析的使用
- Json解析网络请求数据
- 初探Android中的请求服务器并解析返回的json数据
- HTTP请求数据返回解析Josn org.codehaus.jackson.map.JsonMappingException的解决方法