程序包 com.ajaxjs.util
类 JsonUtil
java.lang.Object
com.ajaxjs.util.JsonUtil
Encapsulation of Jackson Library: Conversion Methods Between JSON, Map, Bean, and List.
-
字段概要
字段 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static <T> TconvertValue(Object obj, Class<T> clazz) Converts an object to another object This method uses ObjectMapper from the Jackson library to convert an object of any type to another type specified by clazz It is primarily used for data type conversion when the specific type is known at runtimestatic <T> TConverts a JSON string to a Java object.static <T> TConverts a JSON string to an object of the specified type.static <T> List<T> JSON array string converts to list with Java Beanjson string converts to map This method is used to convert a JSON string into a Map object with String as the key type and Object as the value type.JSON string convert to map with javaBeanjson2mapList(String jsonArrayStr) static com.fasterxml.jackson.databind.JsonNodeJSON string converts to nodestatic <T> LinkedHashMap<String, T> json2sortMap(String jsonStr, Class<T> clazz) JSON string converts to map with javaBean This method converts a JSON string into a LinkedHashMap of String keys and JavaBean values.json2StrMap(String jsonStr) Converts a JSON string to a Map object.static <T> TJava Bean converts to map This method is used to convert a Java Bean object into a Map object, facilitating operations such as lookup, addition, and deletion It does not require detailed explanation for simple delegation to another overload methodJava Bean converts to mapstatic StringConverts a Java object to a JSON string.static StringtoJsonPretty(Object obj) Java Bean, list, array converts to pretty json string
-
字段详细资料
-
OBJECT_MAPPER
public static final com.fasterxml.jackson.databind.ObjectMapper OBJECT_MAPPER
-
-
构造器详细资料
-
JsonUtil
public JsonUtil()
-
-
方法详细资料
-
toJson
Converts a Java object to a JSON string.- 参数:
obj- The Java object to be converted to a JSON string.- 返回:
- The JSON string representation of the Java object.
-
toJsonPretty
Java Bean, list, array converts to pretty json string- 参数:
obj- Java Bean, list, array- 返回:
- The JSON string representation of the Java object.
-
fromJson
Converts a JSON string to an object of the specified type.- 类型参数:
T- The generic type parameter indicating the type of the returned object.- 参数:
jsonStr- The JSON string representing the data to be converted.valueType- The class type of the target object.- 返回:
- The converted object of type T.
- 抛出:
RuntimeException- If the JSON string cannot be converted to the target type.
-
fromJson
Converts a JSON string to a Java object.- 类型参数:
T- the generic type parameter for the return value- 参数:
jsonStr- the JSON string representing the data to be convertedvalueType- the target Java type of the object- 返回:
- the converted Java object
This method uses ObjectMapper to read the JSON string and convert it to the specified Java object type. If any error occurs during the conversion, a runtime exception is thrown.
-
json2map
JSON string convert to map with javaBean- 类型参数:
T- The type of the value in the Map- 参数:
jsonStr- The JSON string to be convertedclazz- The class of the JavaBean type in the Map value- 返回:
- Returns a Map containing keys of type String and values of the specified JavaBean type
-
json2sortMap
JSON string converts to map with javaBean This method converts a JSON string into a LinkedHashMap of String keys and JavaBean values. It uses the Jackson library's ObjectMapper to parse the JSON string according to the specified JavaBean type, and stores it in a LinkedHashMap, preserving the order of insertion.- 类型参数:
T- Generic parameter, represents the type of JavaBean- 参数:
jsonStr- The JSON string to be convertedclazz- The class type of the JavaBean- 返回:
- Returns a LinkedHashMap containing String keys and JavaBean values of the specified type
-
json2map
json string converts to map This method is used to convert a JSON string into a Map object with String as the key type and Object as the value type. It simplifies the process of accessing and manipulating JSON data by converting it into a Map format.- 参数:
jsonStr- The JSON string to be converted.- 返回:
- Returns a Map object containing the key-value pairs converted from the JSON string.
-
json2StrMap
Converts a JSON string to a Map object. This method specifically converts a string in JSON format into a Map object where the key and value are both of type String. It is used to facilitate the parsing and use of JSON data in programs.- 参数:
jsonStr- The JSON string to be converted, which should be in a format that can be correctly parsed into a Map.- 返回:
- Returns a Map object with String keys and values, representing the converted JSON data.
-
json2list
JSON array string converts to list with Java Bean- 类型参数:
T- Generic type parameter, indicating the type of elements in the list- 参数:
jsonArrayStr- The JSON array string, representing a list of objectsclazz- The class type of the list elements, used to specify the type of Java Bean- 返回:
- Returns a list of Java Bean objects converted from the JSON array string
-
json2mapList
-
convertValue
Converts an object to another object This method uses ObjectMapper from the Jackson library to convert an object of any type to another type specified by clazz It is primarily used for data type conversion when the specific type is known at runtime- 类型参数:
T- Generic parameter, represents the target type to convert to- 参数:
obj- The object to be converted, can be of any typeclazz- The target type class to convert to- 返回:
- Returns an object of the converted target type
-
pojo2map
Java Bean converts to map This method is used to convert a Java Bean object into a Map object, facilitating operations such as lookup, addition, and deletion It does not require detailed explanation for simple delegation to another overload method- 参数:
obj- The Java Bean object to be converted- 返回:
- Returns a Map object containing all the properties of the Java Bean
-
pojo2map
Java Bean converts to map- 类型参数:
T- The generic type of the map value- 参数:
obj- The Java Bean object to be convertedclazz- The class type of the map value- 返回:
- Returns a map converted from the Java Bean, with String as the key and the specified generic type as the value
-
map2pojo
-
json2Node
JSON string converts to node- 参数:
jsonStr- The JSON string to be converted- 返回:
- JsonNode object converted from the JSON string
-