JAVA中使用JSON

JSON 语法是 JavaScript 对象表示语法的子集。
数据在键值对中;
键值对由冒号分隔;
数据由逗号分隔;
花括号保存对象;
方括号保存数组;

示例:
{
    "people":[
        {"firstName":"a1","lastName":"b1","email":"c1"},
        {"firstName":"a2","lastName":"b2","email":"c2"},
        {"firstName":"a3","lastName":"b3","email":"c3"}
    ]
}
其中
"firstName":"a1"表示一个数据,firstName是键,a1是值。一个对象中键必须唯一;
{"firstName":"a1","lastName":"b1","email":"c1"}表示一个对象,对象中的数据应该没有关系,是组成一个对象的不同元素;
"people":[...]表示一个数组,数组中元素应该表示同一类对象。

JSONObject与JSONArray区别
区别在于JSONObject是一个{}包裹起来的一个对象(Object),而JSONArray则是[]包裹起来的一个数组(Array),JSONObjec有键名,JSONArray没有。
解析的时候JSONObject是JSONObject.getString("message"),JSONArray是JSONArray.getString(0),0表示取第一个值,1表示取第二个值。

在Java中使用JSON

1、导入JSON-lib及其依赖包;

2、代码中引入
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

3、创建和解析JSON对象

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

public class test {
    // 创建JSONObject对象
    private static JSONObject createJSONObject() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ak", "av");
        jsonObject.put("bk", "bv");
        jsonObject.put("ck", "cv");
        return jsonObject;
    }

    // 创建JSONObject对象
    private static JSONArray createJSONarray() throws JSONException {
        JSONArray jsonArray = new JSONArray();
        jsonArray.put("a");
        jsonArray.put("b");
        jsonArray.put("c");
        jsonArray.put(1);// 数字
        jsonArray.put(true);// boolean
        return jsonArray;
    }

    private static void print(String string) {
        System.out.println(string);
        System.out.println("==========万恶的分隔符==========");
        System.out.println();
    }

    public static void main(String args[]) {
        try {
            System.out.println("创建JSONObject");
            JSONObject testObject = createJSONObject();
            print(testObject.toString());

            System.out.println("创建JSONArray");
            JSONArray testArray = createJSONarray();
            print(testArray.toString());

            System.out.println("键相同时,值会覆盖原有的值");
            testObject.put("ak", "ak");
            print(testObject.toString());

            System.out.println("元素相同,不会覆盖原有的值;值为null时同样可以添加,注意null没有双引号");
            testArray.put("a");
            testArray.put(null);
            print(testArray.toString());

            System.out.println("键相同且值为null时,移除该键值对");
            testObject.put("bk", null);
            print(testObject.toString());

            System.out.println("JSONArray中添加JSONObject");
            testArray.put(createJSONObject());
            print(testArray.toString());

            System.out.println("JSONObject中添加JSONArray");
            testObject.put("JSONArray", createJSONarray());
            print(testObject.toString());

            System.out.println("JSONObject和JSONArray嵌套");
            testObject.put("JSONArray", testArray);
            print(testObject.toString());

            System.out.println("解析JSONObject和JSONArray");
            System.out.println("原始JSONObject对象:" + testObject.toString());
            System.out.println("获取key为ck的值:" + testObject.getString("ck"));
            System.out.println("获取key为JSONArray的JSONArray对象:"
                    + testObject.getJSONArray("JSONArray").toString());
            print("获取key为JSONArray的JSONArray对象中的最后的JSONObject对象的key为bk的值:"
                    + testObject.getJSONArray("JSONArray").getJSONObject(7)
                            .getString("bk"));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

程序运行结果:

创建JSONObject
{"ck":"cv","bk":"bv","ak":"av"}
==========万恶的分隔符==========

创建JSONArray
["a","b","c",1,true]
==========万恶的分隔符==========

键相同时,值会覆盖原有的值
{"ck":"cv","bk":"bv","ak":"ak"}
==========万恶的分隔符==========

元素相同,不会覆盖原有的值;值为null时同样可以添加,注意null没有双引号
["a","b","c",1,true,"a",null]
==========万恶的分隔符==========

键相同且值为null时,移除该键值对
{"ck":"cv","ak":"ak"}
==========万恶的分隔符==========

JSONArray中添加JSONObject
["a","b","c",1,true,"a",null,{"ck":"cv","bk":"bv","ak":"av"}]
==========万恶的分隔符==========

JSONObject中添加JSONArray
{"ck":"cv","JSONArray":["a","b","c",1,true],"ak":"ak"}
==========万恶的分隔符==========

JSONObject和JSONArray嵌套
{"ck":"cv","JSONArray":["a","b","c",1,true,"a",null,{"ck":"cv","bk":"bv","ak":"av"}],"ak":"ak"}
==========万恶的分隔符==========

解析JSONObject和JSONArray
原始JSONObject对象:{"ck":"cv","JSONArray":["a","b","c",1,true,"a",null,{"ck":"cv","bk":"bv","ak":"av"}],"ak":"ak"}
获取key为ck的值:cv
获取key为JSONArray的JSONArray对象:["a","b","c",1,true,"a",null,{"ck":"cv","bk":"bv","ak":"av"}]
获取key为JSONArray的JSONArray对象中的最后的JSONObject对象的key为bk的值:bv
==========万恶的分隔符==========


JSON在线转换工具:http://www.bejson.com/
JSON对象与其他对象相互转换:http://www.cnblogs.com/undead/archive/2012/07/18/2594900.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章