程序包 com.ajaxjs.util

类 JsonUtil


  • public class JsonUtil
    extends Object
    JSON Utils based on Jackson
    • 构造器详细资料

      • JsonUtil

        public JsonUtil()
    • 方法详细资料

      • toJson

        public static String toJson​(Object obj)
        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

        public static String toJsonPretty​(Object obj)
        Java Bean, list, array converts to pretty json string
        参数:
        obj - Java Bean, list, array
        返回:
        The JSON string representation of the Java object.
      • fromJson

        public static <T> T fromJson​(String json,
                                     Class<T> valueType)
        Converts a JSON string to an object of the specified type.
        类型参数:
        T - The generic type parameter indicating the type of the returned object.
        参数:
        json - 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

        public static <T> T fromJson​(String json,
                                     com.fasterxml.jackson.databind.JavaType valueType)
        Converts a JSON string to a Java object.
        类型参数:
        T - the generic type parameter for the return value
        参数:
        json - the JSON string representing the data to be converted
        valueType - 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

        public static <T> Map<String,​T> json2map​(String jsonStr,
                                                       Class<T> clazz)
        JSON string convert to map with javaBean
        类型参数:
        T - The type of the value in the Map
        参数:
        jsonStr - The JSON string to be converted
        clazz - 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

        public static <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. 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 converted
        clazz - The class type of the JavaBean
        返回:
        Returns a LinkedHashMap containing String keys and JavaBean values of the specified type
      • json2map

        public static Map<String,​Object> json2map​(String jsonStr)
        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

        public static Map<String,​String> json2StrMap​(String jsonStr)
        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

        public static <T> List<T> json2list​(String jsonArrayStr,
                                            Class<T> clazz)
        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 objects
        clazz - 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
      • convertValue

        public static <T> T convertValue​(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 runtime
        类型参数:
        T - Generic parameter, represents the target type to convert to
        参数:
        obj - The object to be converted, can be of any type
        clazz - The target type class to convert to
        返回:
        Returns an object of the converted target type
      • pojo2map

        public static Map<String,​Object> pojo2map​(Object obj)
        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

        public static <T> Map<String,​T> pojo2map​(Object obj,
                                                       Class<T> clazz)
        Java Bean converts to map
        类型参数:
        T - The generic type of the map value
        参数:
        obj - The Java Bean object to be converted
        clazz - 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
      • json2Node

        public static com.fasterxml.jackson.databind.JsonNode json2Node​(String json)
        JSON string converts to node
        参数:
        json - The JSON string to be converted
        返回:
        JsonNode object converted from the JSON string