先來看下它有哪些常用方法,以及有什么作用:
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清除某一個鍵值對。
由於JSONObject是一個map,它還具有map特有的兩個方法:
10.Set
11.Set<Map.Entry<String, Object>> entrySet():在循環遍歷時使用,取得是鍵和值的映射關系,Entry就是Map接口中的內部接口
與String字符串轉換:
12.toJSONString() /toString():將JSONObject對象轉換為json的字符串
常用的方法主要為以上這些,下面列出使用這些方法的example:
package com.snake.test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @Author Snake
* @Date 2019/11/24 14:21
* @Version 1.0.0
*/
public class myTest {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("username","snake");
map.put("age","18");
map.put("password","123456");
JSONObject jsonObject = new JSONObject(map);
//jsonObject:{"password":"123456","age":"18","username":"snake"}
System.out.println("jsonObject:"+jsonObject);
jsonObject.remove("age");
//去除一個key-values后的jsonObject:{"password":"123456","username":"snake"}
System.out.println("去除一個key-values后的jsonObject:"+jsonObject);
Object put = jsonObject.put("username", "snake17");
//jsonObject.put:snake
System.out.println("jsonObject.put:"+put);
Object put1 = jsonObject.put("username", "snake27");
//第一次jsonObject.put:snake17
System.out.println("第一次jsonObject.put:"+put1);
//第二次jsonObject.put:snake17
System.out.println("第二次jsonObject.put:"+put1);
JSONObject jsonObject1 = jsonObject.fluentPut("username", "snake107");
//jsonObject1.fluentPut:{"password":"123456","username":"snake107"}
System.out.println("jsonObject1.fluentPut:"+jsonObject1);
Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
//set鍵值對關系:[password=123456, username=snake107]
System.out.println("set鍵值對關系:"+entries);
Map<String, Object> innerMap = jsonObject.getInnerMap();
//map對象:{password=123456, username=snake107}
System.out.println("map對象:"+innerMap);
Set<String> strings = jsonObject.keySet();
//key值:[password, username]
System.out.println("key值:"+strings);
User user = jsonObject.toJavaObject(User.class);
//jsonObject轉對象:User(username=snake107, age=null, password=123456)
System.out.println("jsonObject轉對象:"+user);
String s = jsonObject.toJSONString();
//jsonObject轉jsonString{"password":"123456","username":"snake107"}
System.out.println("jsonObject轉jsonString"+s);
Object parse = JSONObject.parse(s);
//jsonString轉object:{"password":"123456","username":"snake107"}
System.out.println("jsonString轉object:"+parse);
User user1 = JSONObject.parseObject(s, User.class);
//jsonString轉對象:User(username=snake107, age=null, password=123456)
System.out.println("jsonString轉對象:"+user1);
ArrayList<User> users = new ArrayList<>();
users.add(user);
Object usersObject = JSONArray.parse(JSONArray.toJSONString(users));
//usersObject:[{"password":"123456","username":"snake107"}]
System.out.println("usersObject:"+usersObject);
String s1 = JSONObject.toJSONString(usersObject);
ArrayList arrayList = JSONObject.parseObject(s1, users.getClass());
//[{"password":"123456","username":"snake107"}]
System.out.println(arrayList);
JSONArray objects = JSONArray.parseArray(JSONArray.toJSONString(users));
//jsonArray:[{"password":"123456","username":"snake107"}]
System.out.println("jsonArray:"+objects);
}
}
