usingSystem; usingLitjson; stringConfigstr; voidStart () {ArrayList info= LoadFile (Application.datapath,"Config1.txt"); foreach(stringSTRsinchinfo) {Configstr+=STRs; } //string str = @ "// { //"Speed " ": 2// }";jsondata JD=Jsonmapper.toobject (CONFIGSTR); speed= (int) jd[" Speed"];//2} ArrayList LoadFile (stringPathstringname) {StreamReader SR=NULL; Try{ stringfilepath=path+"//"+name; if(File.exists (FilePath)) {SR= File.OpenText (path +"//"+name); } Else { return NULL; } } Catch(Exception e) {return NULL; } stringLine ; ArrayList arrlist=NewArrayList (); while(line = Sr. ReadLine ())! =NULL) {Arrlist.add (line); } Sr. Close (); Sr. Dispose (); returnarrlist; }
Public voidResolvejson () {stringstr =@"{"" Name "": "" Yusong "", "" Age "": "," "Birthday" " : "1986-11-21" "," "" Thumbnail "": [{"" Url "": "" http://xuanyusong.c Om "", "" Height "": "," "Width" ":" "200" "}, { "Url" ":" "http://baidu.com" "," "Height" ": 1024x768," "W Idth "": "" "" "}"}"; Jsondata JD=jsonmapper.toobject (str); Debug.Log ("name ="+ (string) jd["Name"]); Debug.Log ("Age ="+ (int) jd[" Age"]); Debug.Log ("Birthday ="+ (string) jd["Birthday"]); Jsondata Jditems= jd["Thumbnail"]; for(inti =0; i < Jditems.count; i++) {Debug.Log ("URL ="+ jditems[i]["URL"]); Debug.Log ("Height ="+ (int) jditems[i]["Height"]); Debug.Log ("Width ="+ jditems[i]["Width"]); } }
C # Unity read configuration JSON file