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。

发布了92 篇原创文章 · 获赞 38 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/u013184970/article/details/103275336