zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    BFS模版程序
    7.3 GROUP BY的“新”功能
    《ASP.NET》数据绑定—DropDownList、ListBox
    GPS NEMA 0183协议
    使用Microsoft.Office.Interop.Excel时,64位问题
    HDU 1848(sg博弈) Fibonacci again and again
    Codeforces 559A Gerald&#39;s Hexagon 数三角形
    移动mm 话费支付接入过程(ane)
    Android怎样实现毛玻璃效果之Android高级模糊技术
    ECLIPSE android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
  • 原文地址:https://www.cnblogs.com/88999660/p/2954281.html
Copyright © 2011-2022 走看看