天天看點

JsonObject、JsonArray與JSONObject、JSONArray的生成的一些差別和使用總結

通過代碼和運作結果檢視一下我們平時一般都是怎麼使用JsonObject、JsonArray與JSONObject、JSONArray,以及他們之間的一些差別。

首先說一下最基本的差別:

1.JsonObject、JsonArray需要添加gson jar包,通過com.google.gson來導入。

2.JSONObject、JSONArray是Android原生的json類,通過org.json來導入。

1、JsonObject與JSONObject的對比:

String empty1 = null;
        //JsonObject需要添加gson jar包,通過com.google.gson.JsonObject來導入
        JsonObject object = new JsonObject();
        object.addProperty("name","張三");//String字元串
        object.addProperty("id",1);//int 數字
        object.addProperty("boolen",true);//boolen
        object.addProperty("empty1",empty1);//傳空  null
        object.addProperty("empty2","");//傳空  ""
        Log.e("getJson","object="+object.toString());//列印生成的json字元串

        //JSONObject是Android原生的json類,通過import org.json.JSONObject來導入
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","張三");
            jsonObject.put("id",1);
            jsonObject.put("boolen",true);
            jsonObject.put("empty1",null);
            jsonObject.put("empty2","");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject="+jsonObject.toString());
           

 運作結果:

object={"name":"張三","id":1,"boolen":true,"empty1":null,"empty2":""}
jsonObject={"id":1,"empty2":"","boolen":true,"name":"張三"}
           

從上面輸出結果可以看出:

(1)JsonObject使用addProperty指派,怎樣指派就怎樣輸出;

(2)JSONObject使用put指派,會抛出try/catch,輸出結果的順序不一定與指派順序一緻,指派為null的時候沒有輸出結果。

 2、JsonArray與JSONArray的對比

//com.google.gson
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("admin");
        jsonArray.add(true);
        jsonArray.add(1);
        Log.e("getJson","jsonArray="+jsonArray);

        //orj.json
        JSONArray jsonArray1 = new JSONArray();
        //寫法1
        jsonArray1.put("admin");
        jsonArray1.put(true);
        jsonArray1.put(1);
        //寫法2
        try {
            jsonArray1.put(0,"admin");
            jsonArray1.put(1,true);
            jsonArray1.put(2,1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonArray1="+jsonArray1);
           

運作結果:

jsonArray=["admin",true,1]
jsonArray1=["admin",true,1]
           

從上面代碼和運作結果可知:

(1)JsonArray使用add指派;

(2)JSONArray使用put指派,有兩種指派方式,方式2會抛出try/catch。

3、JSONArray裡添加JSONObject、JsonObject

//orj.json
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","張三");
            jsonObject.put("id",1);
            jsonObject.put("boolen",true);
            jsonObject.put("empty1",null);
            jsonObject.put("empty2","");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //orj.json
        JSONArray jsonArray1 = new JSONArray();
        jsonArray1.put("admin");
        jsonArray1.put(true);
        jsonArray1.put(1);
        jsonArray1.put(jsonObject);
        Log.e("getJson","jsonArray1="+jsonArray1);

//==========================================================//
        //com.google.gson
        String empty1 = null;
        JsonObject object = new JsonObject();
        object.addProperty("name","張三");//String字元串
        object.addProperty("id",1);//int 數字
        object.addProperty("boolen",true);//boolen
        object.addProperty("empty1",empty1);//傳空  null
        object.addProperty("empty2","");//傳空  ""
        Log.e("getJson","object="+object.toString());//列印生成的json字元串

        //orj.json
        JSONArray jsonArray2 = new JSONArray();
        jsonArray2.put("admin");
        jsonArray2.put(true);
        jsonArray2.put(1);
        jsonArray1.put(object);
        Log.e("getJson","jsonArray2="+jsonArray2);
           

輸出結果:

jsonArray1=["admin",true,1,{"id":1,"empty2":"","boolen":true,"name":"張三"}]
jsonArray2=["admin",true,1,"{\"name\":\"張三\",\"id\":1,\"boolen\":true,\"empty1\":null,\"empty2\":\"\"}"]
           

從上面輸出結果可以看出:

(1)JSONArray裡添加JSONObject,添加後的JSONObject是正常的object資料;

(2)JSONArray裡添加JsonObject,添加後的JsonObject是一個字元串。

(3)JsonArray.add()裡的添加參數沒有object對象,可以是object.toString(),輸出結果就跟上面jsonArray2的結果一樣了。

4、JSONObject裡添加JSONArray、JsonArray

//com.google.gson
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("admin");
        jsonArray.add(true);
        jsonArray.add(1);

        //orj.json
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("name","李四");
            jsonObject1.put("id",2);
            jsonObject1.put("boolen",false);
            jsonObject1.put("empty3",null);
            jsonObject1.put("empty4","");
            jsonObject1.put("jsonArray",jsonArray);//Android原生的json類裡添加jsonArray
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject1="+jsonObject1);
//=========================================================//

        //orj.json
        JSONArray jsonArray1 = new JSONArray();
        jsonArray1.put("admin");
        jsonArray1.put(true);
        jsonArray1.put(1);

        //orj.json
        JSONObject jsonObject2 = new JSONObject();
        try {
            jsonObject2.put("name","李四");
            jsonObject2.put("id",2);
            jsonObject2.put("boolen",false);
            jsonObject2.put("empty3",null);
            jsonObject2.put("empty4","");
            jsonObject2.put("jsonArray",jsonArray1);//Android原生的json類裡添加JSONArray
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject2="+jsonObject2);
           

運作結果:

jsonObject1={"id":2,"boolen":false,"jsonArray":"[\"admin\",true,1]","empty4":"","name":"李四"}
jsonObject2={"id":2,"boolen":false,"jsonArray":["admin",true,1],"empty4":"","name":"李四"}
           

從上面運作結果可以看出:

(1)JSONObject裡添加JsonArray,添加後的JsonArray變成了一個字元串;

(2)JSONObject裡添加JSONArray,添加後的JSONArray是個數組。

(3)JsonObject.addProperty()裡的添加參數沒有數組,可以是array.toString(),輸出結果就跟上面jsonObject1的結果一樣了。

5、總結:

如果想要在json裡添加array數組或者onject對象,就使用JSONObject和JSONArray。