文章目录
- 一、依赖
- 二、介绍
- 三、JSONObject
- 1、构造方法
- 2、装入
- 3、取出
- 4、删除
- 5、是否包含
- 6、遍历
- 四、JSONArray
- 1、构造方法
- 2、add
- 3、get
- 3、是否包含
- 4、是否空
- 5、清空
- 6、遍历
- 7、获取指定范围内集合
- 五、相互转换
- 1、javaScript
- 2、java
- 1)json串 -> JSONObject
- 2)json串 -> JSONArray
- 3)Object -> json串
- 4)json串 -> Map
- 5)对象 -> json串
- 6)json串 -> 对象
- 7)JSONObject -> 实体类
- 8)JSONArray -> 集合
- 9)JsonArray -> 数组
- 10)集合 -> JSONArray
- 11)json串 -> 集合
- 12)Map -> JSONObject
- 13)JSONObject ->Map
一、依赖
<dependency>
<groupId></groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
二、介绍
- 数据格式
{ "name":"张三", "from":"北京", "age":25 }
{ "name":"张三","department":{"id":"9527", "name":"RD"} }
{ "name": "张三", "skill": ["JAVA", "HTML","CSS", "JS", "MYSQL"] }
[1, 2, 3]
[{"name":"kimi"}, {"name":"sally"}]
三、JSONObject
extends JSON implements Map<String, Object>,Cloneable, Serializable, InvocationHandler
- 和Map的API相似
1、构造方法
new JSONObject();
new JSONObject(Map<String, Object> map);
JSONObject(boolean ordered);
2、装入
Object put(String key, Object value);
void putAll(Map<String,?> m);
V putIfAbsent(K key, V value);
JSONObject fluentPut(String key, Object value);
JSONObject fluentPutAll(Map<? extends String, ?> m);
3、取出
Object get(Object key);
V getOrDefault(Object key, V defaultValue)
T getObject(String key, Class<T> clazz);
JSONObject getJSONObject(String key);
JSONArray getJSONArray(String key);
String getString(String key)
Boolean getBoolean(String key)
boolean getBooleanValue(String key)
Integer getInteger(String key)
int getIntValue(String key)
Long getLong(String key)
long getLongValue(String key)
Double getDouble(String key)
double getDoubleValue(String key)
BigDecimal getBigDecimal(String key)
BigInteger getBigInteger(String key)
Date getDate(String key)
4、删除
Object remove(Object key);
boolean remove(Object key, Object value);
JSONObject fluentRemove(Object key);
5、是否包含
boolean containsKey(Object key);
boolean containsValue(Object value);
6、遍历
Set<Map.Entry<String, Object>> jsonObject.entrySet();
四、JSONArray
extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable
- 和List相似
1、构造方法
new JSONArray();
new JSONArray(List<Object> list);
2、add
boolean add(Object e)
void add(int index, Object element)
boolean addAll(int index, Collection<?> c)
boolean addAll(Collection<?> c)
JSONArray fluentAdd(Object e)
JSONArray fluentAdd(int index, Object element)
JSONArray fluentAddAll(Collection<?> c)
JSONArray fluentAddAll(int index, Collection<?> c)
3、get
Object get(int index)
T getObject(int index, Class<T> clazz);
JSONObject getJSONObject(int index);
JSONArray getJSONArray(int index);
String getString(int index)
Boolean getBoolean(int index)
boolean getBooleanValue(int index)
Integer getInteger(int index)
int getIntValue(int index)
Long getLong(int index)
long getLongValue(int index)
Double getDouble(int index)
double getDoubleValue(int index)
BigDecimal getBigDecimal(int index)
BigInteger getBigInteger(int index)
Date getDate(int index)
3、是否包含
boolean contains(Object o);
boolean containsAll(Collection<?> c);
4、是否空
boolean isEmpty();
5、清空
void clear();
6、遍历
for(int i=0;i<arr.size();i++) {
JSONObject json=arr.getJSONObject(i);
String str=json.getString(i);
}
arr.foreach(obj->{
})
7、获取指定范围内集合
List<E> subList(int fromIndex, int toIndex);
五、相互转换
1、javaScript
//将一个javaScript对象/数组转换为一个json串
let str = (obj)
//将json串转换为javaScript对象/数组
let obj = (str)
2、java
1)json串 -> JSONObject
JSONObject JSONObject.parseObject(String str);
2)json串 -> JSONArray
JSONArray JSONArray.parseArray(String str);
3)Object -> json串
String JSON.toJSONString(Object obj);
4)json串 -> Map
Map<String,Object> JSON.parseObject(jsonStr);
5)对象 -> json串
String JSON.toJSONString(students);
String JSON.toJSONString(Object object, SerializerFeature... features);
6)json串 -> 对象
T JSON.parseObject(String json, Class<T> clazz);
@JSONField(name="es_id");
private String esId;
@JSONField(name="time",format="yyyyMMdd")
7)JSONObject -> 实体类
T toJavaObject(JSON json, Class<T> clazz);
8)JSONArray -> 集合
List<T> toJavaList(Class<T> clazz);
9)JsonArray -> 数组
String[] jsonArray.toArray(new String[jsonArray.size()]);
10)集合 -> JSONArray
JSONArray JSONArray.parseArray(JSON.toJSONString(list));
11)json串 -> 集合
List<Integer> JSONObject.parseArray("[12,13]", Integer.class);
List<JSONObject> JSONObject.parseArray("[{name:'kimi',age:18},{name:'sally',age:20}]",JSONObject.class)
List<User> JSONObject.parseArray("[{name:'kimi',age:18},{name:'sally',age:20}]",User.class)
List<Map<String,String>> JSON.parseObject(str, new TypeReference<ArrayList<Map<String,String>>>(){});
12)Map -> JSONObject
JSONObject new JSONObject(Map<String, Object> map);
13)JSONObject ->Map
Map<String, Object> jsonobject.getInnerMap();