public class JSONUtil extends Object
Constructor and Description |
---|
JSONUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
formatJsonStr(String jsonStr)
格式化JSON字符串,此方法并不严格检查JSON的格式正确与否
|
static JSON |
getByPath(JSON json,
String expression)
通过表达式获取JSON中嵌套的对象
.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值 []表达式,可以获取集合等对象中对应index的值 表达式栗子: persion persion.name persons[3] person.friends[5].name |
static <T> T |
getByPath(JSON json,
String expression,
T defaultValue)
通过表达式获取JSON中嵌套的对象
.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值 []表达式,可以获取集合等对象中对应index的值 表达式栗子: persion persion.name persons[3] person.friends[5].name |
static <T> T |
getByPath(JSON json,
String expression,
Type type)
通过表达式获取JSON中嵌套的对象
.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值 []表达式,可以获取集合等对象中对应index的值 表达式栗子: persion persion.name persons[3] person.friends[5].name |
static <T> T |
getObjByPath(JSON json,
String expression)
通过表达式获取JSON中嵌套的对象
.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值 []表达式,可以获取集合等对象中对应index的值 表达式栗子: persion persion.name persons[3] person.friends[5].name |
static boolean |
isEmpty(JSON json)
JSON对象是否为空,以下情况返回true
null JSON.isEmpty()
|
static boolean |
isTypeJSON(String str)
是否为JSON类型字符串,首尾都为大括号或中括号判定为JSON字符串
|
static boolean |
isTypeJSONArray(String str)
是否为JSONArray类型的字符串,首尾都为中括号判定为JSONArray字符串
|
static boolean |
isTypeJSONObject(String str)
是否为JSONObject类型字符串,首尾都为大括号判定为JSONObject字符串
|
static JSONArray |
ofArray()
创建 JSONArray
|
static JSONArray |
ofArray(JSONConfig config)
创建 JSONArray
|
static JSONObject |
ofObj()
创建JSONObject
|
static JSONObject |
ofObj(JSONConfig config)
创建JSONObject
|
static JSONPrimitive |
ofPrimitive(Object value)
创建JSONPrimitive对象,用于创建非JSON对象,例如:
JSONUtil.ofPrimitive(1);
JSONUtil.ofPrimitive(1L);
JSONUtil.ofPrimitive(1.0);
JSONUtil.ofPrimitive(true);
JSONUtil.ofPrimitive("str");
|
static JSONPrimitive |
ofPrimitive(Object value,
JSONConfig config)
创建JSONPrimitive对象,用于创建非JSON对象,例如:
JSONUtil.ofPrimitive(1, config);
JSONUtil.ofPrimitive(1L, config);
JSONUtil.ofPrimitive(1.0, config);
JSONUtil.ofPrimitive(true, config);
JSONUtil.ofPrimitive("str", config);
|
static JSON |
parse(Object obj)
解析对象为JSON,持的对象:
String: 解析为相应的对象
Number、boolean: 转换为
JSONPrimitive
Array、Iterable、Iterator:转换为JSONArray
Bean对象:转为JSONObject
|
static JSON |
parse(Object obj,
JSONConfig config)
解析对象为JSON,持的对象:
String: 解析为相应的对象
Number、boolean: 转换为
JSONPrimitive
Array、Iterable、Iterator:转换为JSONArray
Bean对象:转为JSONObject
|
static JSON |
parse(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
解析对象为JSON,持的对象:
String: 解析为相应的对象
Number、boolean: 转换为
JSONPrimitive
Array、Iterable、Iterator:转换为JSONArray
Bean对象:转为JSONObject
|
static JSONArray |
parseArray(Object obj)
JSON字符串转JSONArray
|
static JSONArray |
parseArray(Object obj,
JSONConfig config)
JSON字符串转JSONArray
|
static JSONArray |
parseArray(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
对象转JSONArray
|
static JSONObject |
parseFromXml(String xmlStr)
XML字符串解析为
JSONObject |
static JSONObject |
parseObj(Object obj)
JSON字符串转JSONObject对象
此方法会忽略空值,但是对JSON字符串不影响 |
static JSONObject |
parseObj(Object obj,
JSONConfig config)
JSON字符串转JSONObject对象
此方法会忽略空值,但是对JSON字符串不影响 |
static JSONObject |
parseObj(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
对象转JSONObject对象
|
static JSON |
readJSON(File file,
Charset charset)
读取JSON
|
static JSONArray |
readJSONArray(File file,
Charset charset)
读取JSONArray
|
static JSONObject |
readJSONObject(File file,
Charset charset)
读取JSONObject
|
void |
registerTypeAdapter(TypeAdapter typeAdapter)
全局注册自定义类型适配器,用于自定义对象序列化和反序列化
提供的适配器必须为实现 MatcherJSONSerializer 或MatcherJSONDeserializer 接口当两个接口都实现时,同时注册序列化和反序列化器 |
void |
registerTypeAdapter(Type type,
TypeAdapter typeAdapter)
全局注册自定义类型适配器,用于自定义对象序列化和反序列化
|
static <T> T |
toBean(Object json,
Class<T> clazz)
转为实体类对象
|
static <T> T |
toBean(Object obj,
JSONConfig config,
Type type)
转为实体类对象
|
static <T> T |
toBean(Object obj,
Type type)
转为实体类对象
|
static JSON |
toJSON(Object obj)
转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。
|
static JSON |
toJSON(Object obj,
JSONConfig config)
转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。
|
static JSON |
toJSON(Object obj,
JSONConfig config,
Predicate<MutableEntry<Object,Object>> predicate)
转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。
|
static String |
toJsonPrettyStr(Object obj)
转换为格式化后的JSON字符串
|
static String |
toJsonStr(Object obj)
转换为JSON字符串
|
static void |
toJsonStr(Object obj,
Appendable appendable)
转换为JSON字符串并写出到writer
|
static String |
toJsonStr(Object obj,
JSONConfig jsonConfig)
转换为JSON字符串
|
static <T> List<T> |
toList(JSONArray jsonArray,
Class<T> elementType)
将JSONArray转换为Bean的List,默认为ArrayList
|
static <T> List<T> |
toList(String jsonArray,
Class<T> elementType)
将JSONArray字符串转换为Bean的List,默认为ArrayList
|
static String |
toXmlStr(JSON json)
转换为XML字符串
|
static JSONObject |
xmlToJson(String xml)
XML转JSONObject
转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。 |
public static JSONObject ofObj()
public static JSONObject ofObj(JSONConfig config)
config
- JSON配置public static JSONArray ofArray()
public static JSONArray ofArray(JSONConfig config)
config
- JSON配置public static JSONPrimitive ofPrimitive(Object value)
JSONUtil.ofPrimitive(1);
JSONUtil.ofPrimitive(1L);
JSONUtil.ofPrimitive(1.0);
JSONUtil.ofPrimitive(true);
JSONUtil.ofPrimitive("str");
value
- 值public static JSONPrimitive ofPrimitive(Object value, JSONConfig config)
JSONUtil.ofPrimitive(1, config);
JSONUtil.ofPrimitive(1L, config);
JSONUtil.ofPrimitive(1.0, config);
JSONUtil.ofPrimitive(true, config);
JSONUtil.ofPrimitive("str", config);
value
- 值config
- 配置public static JSONObject parseObj(Object obj)
obj
- Bean对象或者Mappublic static JSONObject parseObj(Object obj, JSONConfig config)
obj
- Bean对象或者Mapconfig
- JSON配置public static JSONObject parseObj(Object obj, JSONConfig config, Predicate<MutableEntry<Object,Object>> predicate)
obj
- Bean对象或者Mapconfig
- JSON配置predicate
- 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,Predicate.test(Object)
为true
保留public static JSONArray parseArray(Object obj)
obj
- 数组或集合对象或字符串等public static JSONArray parseArray(Object obj, JSONConfig config)
obj
- 数组或集合对象config
- JSON配置public static JSONArray parseArray(Object obj, JSONConfig config, Predicate<MutableEntry<Object,Object>> predicate)
obj
- 数组或集合对象config
- JSON配置predicate
- index和值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,Predicate.test(Object)
为true
保留public static JSON parse(Object obj)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象public static JSON parse(Object obj, JSONConfig config)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象config
- JSON配置,null
使用默认配置public static JSON parse(Object obj, JSONConfig config, Predicate<MutableEntry<Object,Object>> predicate)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象config
- JSON配置,null
使用默认配置predicate
- 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,
Predicate.test(Object)
为true
保留public static JSONObject parseFromXml(String xmlStr)
JSONObject
xmlStr
- XML字符串JSONObject
public static JSON readJSON(File file, Charset charset) throws IORuntimeException
file
- JSON文件charset
- 编码IORuntimeException
- IO异常public static JSONObject readJSONObject(File file, Charset charset) throws IORuntimeException
file
- JSON文件charset
- 编码IORuntimeException
- IO异常public static JSONArray readJSONArray(File file, Charset charset) throws IORuntimeException
file
- JSON文件charset
- 编码IORuntimeException
- IO异常public static String toJsonPrettyStr(Object obj)
obj
- Bean对象public static String toJsonStr(Object obj)
obj
- 被转为JSON的对象public static String toJsonStr(Object obj, JSONConfig jsonConfig)
obj
- 被转为JSON的对象jsonConfig
- JSON配置public static void toJsonStr(Object obj, Appendable appendable)
obj
- 被转为JSON的对象appendable
- Appendable
public static JSONObject xmlToJson(String xml)
xml
- XML字符串public static <T> T toBean(Object json, Class<T> clazz)
T
- Bean类型json
- JSONObjectclazz
- 实体类public static <T> T toBean(Object obj, Type type)
T
- Bean类型obj
- 对象type
- 实体类对象类型public static <T> T toBean(Object obj, JSONConfig config, Type type)
T
- Bean类型obj
- JSONObjectconfig
- JSON配置type
- 实体类对象类型public static JSON toJSON(Object obj)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象public static JSON toJSON(Object obj, JSONConfig config)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象config
- JSON配置,null
使用默认配置public static JSON toJSON(Object obj, JSONConfig config, Predicate<MutableEntry<Object,Object>> predicate)
JSONPrimitive
JSONArray
JSONObject
obj
- 对象config
- JSON配置,null
使用默认配置predicate
- 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,Predicate.test(Object)
为true
保留public static <T> List<T> toList(String jsonArray, Class<T> elementType)
T
- Bean类型jsonArray
- JSONArray字符串elementType
- List中元素类型public static <T> List<T> toList(JSONArray jsonArray, Class<T> elementType)
T
- Bean类型jsonArray
- JSONArray
elementType
- List中元素类型public static <T> T getObjByPath(JSON json, String expression)
表达式栗子:
persion persion.name persons[3] person.friends[5].name
T
- 值类型json
- JSON
expression
- 表达式JSON.getByPath(String)
public static <T> T getByPath(JSON json, String expression, Type type)
表达式栗子:
persion persion.name persons[3] person.friends[5].name
T
- 值类型json
- JSON
expression
- 表达式type
- 结果类型JSON.getByPath(String)
public static <T> T getByPath(JSON json, String expression, T defaultValue)
表达式栗子:
persion persion.name persons[3] person.friends[5].name
T
- 值类型json
- JSON
expression
- 表达式defaultValue
- 默认值JSON.getByPath(String)
public static JSON getByPath(JSON json, String expression)
表达式栗子:
persion persion.name persons[3] person.friends[5].name
json
- JSON
expression
- 表达式JSON.getByPath(String)
public static String formatJsonStr(String jsonStr)
jsonStr
- JSON字符串public static boolean isEmpty(JSON json)
JSON.isEmpty()
json
- JSONObject或JSONArraypublic static boolean isTypeJSON(String str)
str
- 字符串public static boolean isTypeJSONObject(String str)
str
- 字符串public static boolean isTypeJSONArray(String str)
str
- 字符串public void registerTypeAdapter(Type type, TypeAdapter typeAdapter)
type
- 类型typeAdapter
- 自定义序列化器,null
表示移除public void registerTypeAdapter(TypeAdapter typeAdapter)
MatcherJSONSerializer
或MatcherJSONDeserializer
接口typeAdapter
- 自定义类型适配器Copyright © 2025. All rights reserved.