Unity3d request json data and parse

Source: Internet
Author: User

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> // summary of GetPhotoList /// </summary> public class GetPhotoList: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/pla In "; 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) {// get the directory string local Path = path + id + "\"; // read the file in the directory 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 ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.