unity requests json data and parses it

When unity3d is communicating with. net over http, the most common thing is to submit the form data request, but the server will return a lump of json data, which requires us to process the json data in unit. Generally, the most common way to process the json data in unit is litjson (it is the class library that processes the SON database under the. net platform). Below I post the source code, only for learning reference!

For the installation and use of LitJSON, please refer to: http://www.360doc.com/content/13/0117/11/10941785_260686840.shtml

Or refer to: http://blog.csdn.net/dingxiaowei2013/article/details/17115665


Put LitJson.dll in the plugins file in the assets directory. If there is no plugins file, create one manually


Client:

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using LitJson;  
  4.   
  5. public class GetPhotoList : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.         StartCoroutine(GetPhotos());  
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     IEnumerator GetPhotos(){      
  14.         WWWForm    form = new WWWForm();  
  15.         form.AddField("id","123");  
  16.         WWW w = new WWW("http://localhost:36944/GetPhotoList.ashx",form);  
  17.         while (!w.isDone){yield return new WaitForEndOfFrame();}  
  18.         if (w.error != null){Debug.LogError(w.error);}  
  19.         Debug.Log(w.text);          
  20.         JsonData jd = JsonMapper.ToObject(w.text);  
  21.         for (int i = 0; i < jd.Count; i++)  
  22.         {              
  23.             Debug.Log("id=" + jd[i]["id"]);  
  24.             Debug.Log("name=" + jd[i]["name"]);  
  25.         }  
  26.           
  27.     }  
  28. }  

Server:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Runtime.Serialization.Json;  
  6. using System.ServiceModel;  
  7. using System.ServiceModel.Web;  
  8. using System.IO;  
  9.   
  10. namespace UpdatePhoto  
  11. {  
  12.     /// <summary>  
  13.     ///Summary description for GetPhotoList
  14.     /// </summary>  
  15.     public class GetPhotoList : IHttpHandler  
  16.     {  
  17.   
  18.         public void ProcessRequest(HttpContext context)  
  19.         {  
  20.             context.Response.ContentType = "text/plain";  
  21.             string id = context.Request.Form["id"];  
  22.             string path = context.Request.PhysicalApplicationPath;  
  23.             //context.Response.Write("Hello World");  
  24.             List<Photo> photos = GetPhotos(id,path);  
  25.             DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());  
  26.             djson.WriteObject(context.Response.OutputStream, photos);  
  27.         }  
  28.   
  29.         public List<Photo> GetPhotos(string id,string path)  
  30.         {  
  31.             //Get directory
  32.             string localPath = path+id + "\\";   
  33.             //Read files in directory
  34.             if (!Directory.Exists(localPath)) return null;  
  35.             string[] files = Directory.GetFiles(localPath);  
  36.             List<Photo> photos = new List<Photo>();  
  37.             foreach (string file in files)  
  38.             {  
  39.                 string filename = file.Substring(file.LastIndexOf('\\')+1);  
  40.                 Photo p = new Photo();  
  41.                 p.name = filename;  
  42.                 p.id = id;  
  43.                 photos.Add(p);  
  44.             }  
  45.   
  46.   
  47.             return photos;  
  48.         }  
  49.   
  50.         public bool IsReusable  
  51.         {  
  52.             get  
  53.             {  
  54.                 return false;  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     public class Photo  
  60.     {  
  61.         public string id;  
  62.         public string name;  
  63.     }  
  64. }  

Tags: JSON Database

Posted on Mon, 11 May 2020 12:53:01 -0400 by lanmonkey