昆明免費(fèi)網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷的招聘信息
目錄
1.使用的場景區(qū)別
2. 使用方法區(qū)別
3.獲取方式不同
4. 解析JSON字符串
5.總結(jié)
1.使用的場景區(qū)別
- 想通過鍵值對的形式獲取數(shù)據(jù),使用JSONObject。
- 如果后臺查詢的是某個(gè)bean的list集合向前端頁面?zhèn)鬟f,使用JSONArray。
2. 使用方法區(qū)別
?創(chuàng)建方法不同:
?JSONObject創(chuàng)建的方法:
//創(chuàng)建JsonObject第一種方法
JSONObject jsonObject = new JSONObject();
jsonObject.put("UserName", "kobi");
jsonObject.put("age", "34");
jsonObject.put("workIn", "ALI");
System.out.println("jsonObject1:" + jsonObject);//創(chuàng)建JsonObject第二種方法
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("UserName", "ZHULI");
hashMap.put("age", "30");
hashMap.put("workIn", "ALI");
System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
JSONArray創(chuàng)建的方法
//創(chuàng)建一個(gè)JsonArray方法1
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "kobi");
jsonArray.add(1, "34");
jsonArray.add(2, "ALI");
System.out.println("jsonArray1:" + jsonArray);//創(chuàng)建JsonArray方法2
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("kobi");
arrayList.add("34");
arrayList.add("ALI");
System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
3.獲取方式不同
- 獲取JSONObject中值:String userName = jsonObject.getString("UserName");
- 獲取JSONArray中的值:String userName = arrayList.getString("2");
package com.yunos.tv.video.resource.controller.web;import java.util.ArrayList;
import java.util.HashMap;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;public class Test {public static void main(String[] args) {//JsonObject和JsonArray區(qū)別就是JsonObject是對象形式,JsonArray是數(shù)組形式//創(chuàng)建JsonObject第一種方法JSONObject jsonObject = new JSONObject();jsonObject.put("UserName", "ZHULI");jsonObject.put("age", "30");jsonObject.put("workIn", "ALI");System.out.println("jsonObject1:" + jsonObject);//創(chuàng)建JsonObject第二種方法HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put("UserName", "ZHULI");hashMap.put("age", "30");hashMap.put("workIn", "ALI");System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));//創(chuàng)建一個(gè)JsonArray方法1JSONArray jsonArray = new JSONArray();jsonArray.add(0, "ZHULI");jsonArray.add(1, "30");jsonArray.add(2, "ALI");System.out.println("jsonArray1:" + jsonArray);//創(chuàng)建JsonArray方法2ArrayList<String> arrayList = new ArrayList<String>();arrayList.add("ZHULI");arrayList.add("30");arrayList.add("ALI");System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));//如果JSONArray解析一個(gè)HashMap,則會將整個(gè)對象的放進(jìn)一個(gè)數(shù)組的值中System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));//組裝一個(gè)復(fù)雜的JSONArrayJSONObject jsonObject2 = new JSONObject();jsonObject2.put("UserName", "ZHULI");jsonObject2.put("age", "30");jsonObject2.put("workIn", "ALI");jsonObject2.element("Array", arrayList);System.out.println("jsonObject2:" + jsonObject2);}
}
輸出結(jié)果:
jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"workIn":"ALI","age":"30","UserName":"ZHULI"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","30","ALI"]
jsonArray FROM HASHMAP:[{"workIn":"ALI","age":"30","UserName":"ZHULI"}]
jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}
4. 解析JSON字符串
package com.yunos.tv.video.resource.controller.web;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;public class Test {public static void main(String[] args) {String jsonString = "{\"UserName\":\"kobi\",\"age\":\"34\",\"workIn\":\"ALI\",\"Array\":[\"kobi\",\"34\",\"ALI\"]}";//將Json字符串轉(zhuǎn)為java對象JSONObject obj = JSONObject.fromObject(jsonString);//獲取Object中的UserNameif (obj.has("UserName")) {System.out.println("UserName:" + obj.getString("UserName"));}//獲取ArrayObjectif (obj.has("Array")) {JSONArray transitListArray = obj.getJSONArray("Array");for (int i = 0; i < transitListArray.size(); i++) {System.out.print("Array:" + transitListArray.getString(i) + " ");}}}
}
返回結(jié)果:
UserName:kobi
Array:kobi Array:34 Array:ALI
5.總結(jié)
(1).區(qū)別在于JSONObject是一個(gè){}包裹起來的一個(gè)對象(Object),而JSONArray則是[]包裹起來的一個(gè)數(shù)組(Array),說白點(diǎn)就是一個(gè)是數(shù)組一個(gè)是對象或字符串
(2).JSON有兩種結(jié)構(gòu):對象和數(shù)組。
- 對象結(jié)構(gòu)以”{”大括號開始,以”}”大括號結(jié)束。中間部分由0或多個(gè)以”,”分隔的”key(關(guān)鍵字)/value(值)”對構(gòu)成,關(guān)鍵字和值之間以”:”分隔,語法結(jié)構(gòu)如代碼。
{ "1": "2", "4": "5" }
其中關(guān)鍵字是字符串,而值可以是字符串,數(shù)值,true,false,null,對象或數(shù)組 - 數(shù)組結(jié)構(gòu)以”[”開始,”]”結(jié)束。中間由0或多個(gè)以”,”分隔的值列表組成,語法結(jié)構(gòu)如代碼。
?[ { "1": "2", "4": "5" }, { "1": "2", "4": "5" } ]
借鑒:JSONObject和JSONArray區(qū)別及基本用法 - 簡書