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