using UnityEngine; using System.Collections; using System.Collections.Generic ; using JsonFx ; using JsonFx.Json ; public class TestJson : MonoBehaviour { // Use this for initialization void Start () { //封装
// //string json = setJsonArray() ; //解析 //getJsonArray(json) ;
// 初始化model 序列成json string mm = setObject() ;
// 将json映射成model getObject(mm) ; } // Update is called once per frame void Update () { } string setJsonArray() { Hashtable one = new Hashtable() ; one.Add("one" , "oooooooooooooo") ; string[] strs = {"111","2222"}; List<Hashtable> list = new List<Hashtable>() ; list.Add( one ) ; list.Add( one ) ; List<string> listStr = new List<string>() ; listStr.Add("1111") ; listStr.Add("2222") ; Hashtable ht = new Hashtable() ; ht.Add("name" , "xixi" ) ; ht.Add("gender", "male") ; ht.Add("one",one) ; ht.Add("strs",strs) ; ht.Add("list",list) ; ht.Add("listStr",listStr) ; Debug.Log( JsonWriter.Serialize( ht) ) ; //{"list":[{"one":"oooooooooooooo"},{"one":"oooooooooooooo"}],"strs":["111","2222"],"gender":"male","one":{"one":"oooooooooooooo"},"name":"xixi","listStr":["1111","2222"]} return JsonWriter.Serialize( ht ) ; } void getJsonArray( string json ){ // about hashtable http://www.cnblogs.com/liuwenjun830/archive/2006/07/28/462182.html Dictionary<string,object> ht = JsonReader.Deserialize<Dictionary<string,object>>( json ) ; Debug.Log( ht.Count ) ; // is exit or not Debug.Log( ht.ContainsKey("name") ); // out: True Debug.Log( ht.ContainsKey("name11") ); // out : False Debug.Log(ht["name"].ToString() ) ; // out : xixi //Debug.Log(ht["name11"].ToString() ) ; // out : error -- NullReferenceException: Object reference not set to an instance of an objec // get string Debug.Log(ht["name"].ToString() ) ; // get string[] string[] str = (string[]) ht["strs"] ; Debug.Log(" str lenth : " + str.Length + " 11 : " + str[0] ) ; // get Dictionary Dictionary<string,object> one = ( Dictionary<string,object>) ht["one"] ; Debug.Log(" one : " + one["one"].ToString() ) ; // out : one : oooooooooooooo Dictionary<string,object>[] list = ( Dictionary<string,object>[]) ht["list"] ; Debug.Log(" list length : " +list.Length + "----" + list[0]["one"].ToString()) ; //out : list length : 2----oooooooooooooo } // string setObject() { ManModel mm = new ManModel() ; mm.name = "A JUN" ; mm.gender = "boy" ; mm.age = "12" ; ManModel wife = new ManModel() ; wife.name = " Kear" ; wife.gender = "gril" ; wife.age = "21" ; mm.wife = wife ; ManModel friendOne = new ManModel() ; friendOne.name = " F one" ; friendOne.gender = "gril" ; friendOne.age = "222" ; ManModel friendTwo = new ManModel() ; friendTwo.name = " daomei" ; friendTwo.gender = "boy" ; friendTwo.age = "2333" ; List<ManModel> friends = new List<ManModel>() ; friends.Add( friendOne ) ; friends.Add( friendTwo ) ; mm.friends = friends ; Debug.Log ( JsonWriter.Serialize( mm ) ) ; // {"name":"A JUN","gender":"boy","age":"12","address":null,"wife":{"name":" Kear","gender":"gril","age":"21","address":null,"wife":null,"friends":null},"friends":[{"name":" F one","gender":"gril","age":"222","address":null,"wife":null,"friends":null},{"name":" daomei","gender":"boy","age":"2333","address":null,"wife":null,"friends":null}]} return JsonWriter.Serialize( mm ) ; } void getObject( string json ) { ManModel mm = JsonReader.Deserialize<ManModel>( json ) ; Debug.Log( mm.name ) ; // out : A JUN Debug.Log( mm.address== null ) ; // out : True Debug.Log( mm.wife.name) ; // out : Kear Debug.Log(mm.friends[0].name ) ; // out : F one } } public class ManModel { public string name ; public string gender ; public string age ; public string address ; public ManModel wife ; public List<ManModel> friends ; }