1. 程式人生 > >C#中LitJson轉Json使用方法與坑

C#中LitJson轉Json使用方法與坑

    void Start()
    {
        
        //使用JsonData來處理生成json: { "name":"peiandsky","age":28,"sex":"male"}
        JsonData data = new LitJson.JsonData();
        data["name"] = "小李";
        data["age"] = 28;
        data["sex"] = "male";
        string json = data.ToJson();
        //.物件中巢狀物件:{"name":"peiandsky","info":{"sex":"male","age":28}}
        JsonData data2 = new LitJson.JsonData();
        data2["name"] = "peiandsky";
        data2["info"] = new JsonData();
        data2["info"]["sex"] = "male";
        data2["info"]["age"] = 28;
        string json2 = data2.ToJson();
        //將上述兩種方式的Json解析到JsonData
        JsonData jsonData2 = JsonMapper.ToObject(json2);
        Debug.Log(jsonData2["name"] + "   " + data2["info"]["sex"]);
       
        // 使用JsonMapper來處理Json
        Player player = new Player();
        player.name = "peiandsky";
        player.age = 23;
        player.sex = "male";
        player.a = 2.5;
        string json4 = JsonMapper.ToJson(player);
        //解析Player的Json
        Debug.Log(json4);
        Player player2 = JsonMapper.ToObject<Player>(json4);
        Debug.Log(player2.age);
        Debug.Log( player2.name);


    
        // 使用最原始的方式生成Json
        //將陣列轉換成json:["one", "two", "three", "four"]
        JsonWriter writer = new JsonWriter();
        writer.WriteArrayStart();
        writer.Write("one");
        writer.Write("two");
        writer.Write("three");
        writer.Write("four");
        writer.WriteArrayEnd();
        Debug.Log(writer);
       
        //將複合物件轉換成json字串:{"book":{"title":"android game!","author":"pei","bookdetail":{"pages":429,"about":null}}}
        //這種方式非常不方便,不建議使用。
        JsonWriter writer2 = new JsonWriter();
        writer2.WriteObjectStart();
        writer2.WritePropertyName("book");
        writer2.WriteObjectStart();
        writer2.WritePropertyName("title");
        writer2.Write("android game!");
        writer2.WritePropertyName("author");
        writer2.Write("pei");
        writer2.WritePropertyName("bookdetail");


        writer2.WriteObjectStart();
        writer2.WritePropertyName("pages");
        writer2.Write(429);
        writer2.WritePropertyName("about");
        writer2.Write(null);
        writer2.WriteObjectEnd();


        writer2.WriteObjectEnd();


        writer2.WriteObjectEnd();
        Debug.Log(writer2.ToString ());
         
        //在使用LitJson中,建議使用JsonData,JsonMapper來處理Json的編碼和解析。
        Player2 play2 = new global::Player2();
        play2.name = "黃玉磊";
        play2.age = 15;
        play2.score = 25;
        play2.birth = "誰用誰知道!";
       Player2[] P_array = { play2, play2, play2 };
        string json_array = JsonMapper.ToJson(P_array);
        Debug.Log(json_array);


        JsonData pa1 = JsonMapper.ToObject(json_array);
        Debug.Log(pa1.IsArray +"      "+ pa1.Count);


        for (int i = 0; i < pa1.Count; i++)
        {
            Debug.Log(pa1[i]["name"]+"-"+pa1[i]["age"]+"-"+pa1[i]["score"]+"-"+pa1[i]["birth"]);
            int age = int.Parse(pa1[i]["age"].ToString ());
            Debug.Log(age);
        }


        
    }