一.在項目中添加maven依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
二:用法:
1.put(String key, Object value)方法,在JSONObject對象中設定鍵值對在,在進行設值得時候,key是唯一的,如果用相同的key不斷設值得時候,保留後面的值。
2.Object get(String key) :根據key值擷取JSONObject對象中對應的value值,擷取到的值是Object類型,需要手動轉化為需要的資料類型
3.int size():擷取JSONObject對象中鍵值對的數量
4.boolean isEmpty():判斷該JSONObject對象是否為空
5.containsKey(Object key):判斷是否有需要的key值
6.boolean containsValue(Object value):判斷是否有需要的value值
7.JSONObject getJSONObject(String key):如果JSONObjct對象中的value是一個JSONObject對象,即根據key擷取對應的JSONObject對象;
8.JSONArray getJSONArray(String key) :如果JSONObject對象中的value是一個JSONObject數組,既根據key擷取對應的JSONObject數組;
9.Object remove(Object key):根據key清除某一個鍵值對。(key可以為不存在)
由于JSONObject是一個map,它還具有map特有的兩個方法:
10.Set<String> keySet() :擷取JSONObject中的key,并将其放入Set集合中
11.Set<Map.Entry<String, Object>> entrySet():在循環周遊時使用,取得是鍵和值的映射關系,Entry就是Map接口中的内部接口
與String字元串轉換:
12.toJSONString() /toString():将JSONObject對象轉換為json的字元串
13.parseObject()/parseArray():将字元串轉為Json
三,測試代碼:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
//建立一個json對象
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","zhangsan");
jsonObject.put("age",15);
jsonObject.put("sex","nan");
//1.通過json key 擷取value
String name = jsonObject.getString("name");
System.out.println(name);
//2.擷取json的長度
int size = jsonObject.size();
System.out.println(size);
//4.判斷是否為空
boolean b = jsonObject.isEmpty();
System.out.println(b);
//5.是否包含對應的key值,包含傳回true,不包含傳回false
boolean isContainKey = jsonObject.containsKey("name");
System.out.println(isContainKey);
//6.是否包含對應的value值,包含傳回true,不包含傳回false
boolean isContainValue = jsonObject.containsValue("zhangsan");
System.out.println(isContainValue);
//7.JSONObjct對象中的value是一個JSONObject對象,即根據key擷取對應的JSONObject對象;
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("person",jsonObject);
JSONObject person = jsonObject1.getJSONObject("person");
System.out.println(person);
//8.如果JSONObject對象中的value是一個JSONObject數組,既根據key擷取對應的JSONObject數組;
JSONObject jsonObject3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"this is first");
jsonArray.add(1,"this is second");
jsonArray.add(2,"this is third");
jsonObject3.put("testArray",jsonArray);
JSONArray jsonArray1 = jsonObject3.getJSONArray("testArray");
System.out.println(jsonArray1);
//9.remove.根據key移除JSONObject對象中的某個鍵值對,key可以不存在
person.remove("name");
System.out.println(person);
//10.取得JSONObject對象中key的集合,擷取value同理
Set<String> keySet = jsonObject.keySet();
for(String key : keySet){
System.out.print(" " + key);
}
System.out.println();
//11.取得JSONObject對象中的鍵和值的映射關系
Set<Map.Entry<String,Object>> entrySet = jsonObject.entrySet();
for (Map.Entry<String,Object> entry : entrySet){
System.out.println(entry);
System.out.println(entry.getKey() +" "+entry.getValue());
}
//12.轉換為json字元串
String str1 = jsonObject.toJSONString();
String strArry = jsonArray.toJSONString();
System.out.println(str1);
System.out.println(strArry);
//13.将string類型的轉換為json
JSONObject jsonObject2 = JSONObject.parseObject(str1);
JSONArray jsonArray2 = JSONArray.parseArray(strArry);
System.out.println(jsonObject2);
System.out.println(jsonArray2);
}
}