【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1928|回复: 0

[多人在线技术] u3d请求json数据并解析

[复制链接]
发表于 2014-7-7 22:19:05 | 显示全部楼层 |阅读模式
unity3d在跟.net进行http通信的时候,最常见的就是表单数据的提交请求了,但服务器端会返回一坨json数据,这就要求我们在unity中进行json数据的处理了,一般unity中处理json个数数据用的最多的就是LitJSON(它是.net平台下处理SON数据库的类库)。下面我就贴出源码,仅供学习参考!
Client:[mw_shl_code=csharp,true]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[id]);
            Debug.Log(name= + jd[name]);
        }
         
    }
}[/mw_shl_code]
Server:[mw_shl_code=csharp,true]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;
    }
}</photo></photo></photo></photo>[/mw_shl_code]

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 00:59

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表