unity3d请求json数据并解析







Client:

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:

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;
    }
}







unity3d--json传递中文错问问题解决

论坛里看到了一种web下替代xml的解决方案--json,json是一种轻量级的数据交换格式,类似于xml,但是比xml更加轻便,一般用在ajax的数据传输。

但是照着论坛里的方式做了一遍,却出现了问题

在u3d中用eval把得到的字符串放进hashtable时,却报错,把php文件和js文件都改成utf-8也继续报错

很是焦虑

后来无意中发现了u3d中的脚本编辑器有一个utf-8 with BOM,对这个bom产生了怀疑

查了一下

UTF-8之BOM含义,UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是告诉编辑器当前文件采用何种编码,方便编辑器识别,但是BOM虽然在编辑器中不显示,但是会产生输出,就像多了一个空行,没想到困惑我很久的居然是UTF-8编码的BOM,之前在部分采用UTF-8编码的页面会平白多出一个空行,使用DREAMWEAVER编辑网页的时候,采用UTF-8编码时会自动为网页添加BOM签名,于是问题就产生了,使用最新的EDITPLUS 2.30版可以去除BOM,这样,我的网页就不再多出一个空行,但非常奇怪的问题就是,我之前用PHP生成的静态页面没有空行,而采用HTML文件作为模版的网页会自动多出一个空行

有这么个解释,貌似是不建议使用bom方式的utf-8编码,可是记事本另存为居然没有这一项

用ue打开,另存为时,在选择格式时,有一项utf-8 without BOM,确定,哈哈哈哈哈

狂喜中,居然成功了,看来都是编码格式惹的祸


痛定思痛,如果要用这种数据交换格式来下载和上传数据时,一定要注意每一个脚本文件的编码格式









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值