c# - Unity3d,integrate correctly with a json api? -


i'm pretty new c# , unity i'm trying create class able talk external server via json api.

it fine until saw code , repeating same pieces on , on (basically each method of api had ienumerator function json.parse part.

so started thinking have been better create single function get_json , make able return api request in form of object. hit wall , literally have no idea how go ahead, appreciate on how make correct:

using unityengine; using system.collections; using simplejson;   public class site : monobehaviour {      /*         base url http://example.com/         * room:    /ajax/api/?method=get&hashtag=hashtag         * create room: /ajax/api/?method=create&hashtag=hashtag&token=token (optional)         * delete room: /ajax/api/?method=delete&hashtag=hashtag&token=token         * list rooms:    /ajax/api/?method=list_rooms         * list hashtags: /ajax/api/?method=list_hashtags      */      private string base_url = "http://example.com/ajax/api/?method=";     private string q = "";      ienumerator get_json(string q){          var url = base_url+q;         www www = new www(url);         yield return www;          debug.log(www.text);          if (www.error == null){              var resource = json.parse(www.text);              if(int.parse(resource["code"]) == 200){                 debug.log("all right");             }             else{                 debug.log(resource["code"]);             }          }         else{             debug.log("failed http request:\n"+url+"\n"+www.error);         }          yield return null;      }       // use initialization      void start () {         startcoroutine(get_json("list_rooms"));      }      // update called once per frame     void update () {      } } 


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -