public class Convert
extends cn.hutool.core.convert.Convert
提供简单全面的类型转换,适合更多的业务场景,内置hutool、fastjson、yue三种类型转换器,判断精确性能强大,未知类型兼容性更强
| 构造器和说明 |
|---|
Convert() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> T |
convert(Class<T> type,
Object value)
已过时。
|
static <T> T |
convert(Class<T> type,
Object value,
T defaultValue)
转换值为指定类型
|
static <T> T |
convert(Object value,
Class<T> clazz)
转换值为指定类型
(推荐) |
static <T> T |
convert(Type type,
Object value)
转换值为指定类型
|
static <T> T |
convert(Type type,
Object value,
T defaultValue)
转换值为指定类型
|
static <T> T |
convert(cn.hutool.core.lang.TypeReference<T> reference,
Object value)
转换值为指定类型
|
static <T> T |
convertByClassName(String className,
Object value)
转换值为指定类型,类型采用字符串表示
|
static <T> T |
convertQuietly(Type type,
Object value)
转换值为指定类型,不抛异常转换
当转换失败时返回 null |
static <T> T |
convertQuietly(Type type,
Object value,
T defaultValue)
转换值为指定类型,不抛异常转换
当转换失败时返回默认值 |
static <T> T |
toJavaBean(Object value,
Class<T> clazz)
转换值为指定 POJO 类型
|
static com.alibaba.fastjson.JSONArray |
toJSONArray(Object value)
转换为 JSONArray
|
static List<com.alibaba.fastjson.JSONObject> |
toJsonList(com.alibaba.fastjson.JSONArray jsonArray)
|
static List<com.alibaba.fastjson.JSONObject> |
toJsonList(List<Map<String,Object>> list)
|
static <T> List<com.alibaba.fastjson.JSONObject> |
toJsonListT(List<T> list)
|
static com.alibaba.fastjson.JSONObject |
toJSONObject(Object value)
转换为 JSONObject
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(com.alibaba.fastjson.JSONArray jsonArray)
JSONArray 转 JSONObject[]
对象引用转换,内存指针依旧指向元数据
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(List<com.alibaba.fastjson.JSONObject> list)
List-JSONObject 转 JSONObject[]
对象引用转换,内存指针依旧指向元数据
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(String jsonString)
String 转 JSONObject[]
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(String text,
String regex,
String key)
String 转 JSONObject[]
示例:
String text = "1,3,5,9";
JSONObject[] jsons = toJsons(text, ",", "id");
System.out.println(Arrays.toString(jsons));
结果:
[{"id":"1"}, {"id":"3"}, {"id":"5"}, {"id":"9"}]
|
static <T> com.alibaba.fastjson.JSONObject[] |
toJsonsT(List<T> list)
|
static <T> com.alibaba.fastjson.JSONObject[] |
toJsonsTAndRemoveEmpty(List<T> list)
|
static <T> List<T> |
toList(com.alibaba.fastjson.JSONArray jsonArray,
Class<T> clazz)
|
static <T> List<T> |
toList(List<com.alibaba.fastjson.JSONObject> list,
Class<T> clazz)
|
static List<String> |
toList(List<com.alibaba.fastjson.JSONObject> list,
String keepKey)
|
static <T> List<T> |
toList(List<com.alibaba.fastjson.JSONObject> list,
String keepKey,
Class<T> clazz)
|
static <T> ArrayList<T> |
toList(T[] array)
数组转List
此方法为 Arrays.asList(Object...)
|
static List<String> |
toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list,
String keepKey)
|
static <T> List<T> |
toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list,
String keepKey,
Class<T> clazz)
|
static <T> T |
toObject(Object value,
Class<T> clazz)
转换值为指定类型
|
bytesToInt, bytesToLong, bytesToShort, byteToUnsignedInt, convertCharset, convertTime, convertWithCheck, digitToChinese, hexStrToStr, hexToBytes, hexToStr, intToByte, intToBytes, longToBytes, numberToChinese, numberToWord, shortToBytes, strToUnicode, toBigDecimal, toBigDecimal, toBigInteger, toBigInteger, toBool, toBool, toBooleanArray, toByte, toByte, toByteArray, toChar, toChar, toCharArray, toCollection, toDate, toDate, toDBC, toDBC, toDouble, toDouble, toDoubleArray, toEnum, toEnum, toFloat, toFloat, toFloatArray, toHex, toHex, toInstant, toInt, toInt, toIntArray, toList, toList, toLocalDateTime, toLocalDateTime, toLong, toLong, toLongArray, toMap, toNumber, toNumber, toNumberArray, toPrimitiveByteArray, toSBC, toSBC, toShort, toShort, toShortArray, toStr, toStr, toStrArray, unicodeToStr, unWrap, wrappublic static <T> T convertByClassName(String className, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型className - 类的字符串表示value - 值cn.hutool.core.convert.ConvertException - 转换器不存在@Deprecated public static <T> T convert(Class<T> type, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(cn.hutool.core.lang.TypeReference<T> reference,
Object value)
throws cn.hutool.core.convert.ConvertException
T - 目标类型reference - 类型参考,用于持有转换后的泛型类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Type type, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Class<T> type, Object value, T defaultValue) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值defaultValue - 默认值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Type type, Object value, T defaultValue) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值defaultValue - 默认值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convertQuietly(Type type, Object value)
nullT - 目标类型type - 目标类型value - 值public static <T> T convertQuietly(Type type, Object value, T defaultValue)
T - 目标类型type - 目标类型value - 值defaultValue - 默认值public static <T> T convert(Object value, Class<T> clazz)
(推荐)T - 泛型value - 被转换的值clazz - 泛型类型toObject(Object, Class)public static <T> T toObject(Object value, Class<T> clazz)
T - 泛型value - 被转换的值clazz - 泛型类型public static <T> T toJavaBean(Object value, Class<T> clazz)
T - 泛型value - 被转换的值clazz - 泛型类型public static com.alibaba.fastjson.JSONObject toJSONObject(Object value)
value - 被转换的值public static com.alibaba.fastjson.JSONArray toJSONArray(Object value)
value - 被转换的值public static <T> ArrayList<T> toList(T[] array)
此方法为 Arrays.asList(Object...) 的安全实现
T - 数组中的对象类array - 将被转换的数组ListUtils.toList(Object[])public static <T> List<T> toList(com.alibaba.fastjson.JSONArray jsonArray, Class<T> clazz)
T - 泛型jsonArray - 需要转换的JSONArrayclazz - json转换的POJO类型public static <T> List<T> toList(List<com.alibaba.fastjson.JSONObject> list, Class<T> clazz)
T - 泛型list - 需要转换的Listclazz - json转换的POJO类型ListUtils.toList(List, Class)public static List<String> toList(List<com.alibaba.fastjson.JSONObject> list, String keepKey)
list - 需要转换的ListkeepKey - 保留值的keyListUtils.toList(List, String)public static <T> List<T> toList(List<com.alibaba.fastjson.JSONObject> list, String keepKey, Class<T> clazz)
T - 泛型list - 需要转换的ListkeepKey - 保留值的keyclazz - 类型ListUtils.toList(List, String, Class)public static List<String> toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list, String keepKey)
list - 需要转换的ListkeepKey - 保留值的keyListUtils.toListAndDistinct(List, String)public static <T> List<T> toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list, String keepKey, Class<T> clazz)
T - 泛型list - 需要转换的ListkeepKey - 保留值的keyclazz - 类型ListUtils.toListAndDistinct(List, String, Class)public static List<com.alibaba.fastjson.JSONObject> toJsonList(List<Map<String,Object>> list)
list - 需要转换的ListListUtils.toJsonList(List)public static List<com.alibaba.fastjson.JSONObject> toJsonList(com.alibaba.fastjson.JSONArray jsonArray)
性能测试报告:
无类型转换(类型推断):见 toJsonList(List)
安全模式强制类型转换:暂未测试
jsonArray - 需要转换的JSONArrayListUtils.toJsonList(JSONArray)public static <T> List<com.alibaba.fastjson.JSONObject> toJsonListT(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonListT(List)public static com.alibaba.fastjson.JSONObject[] toJsons(com.alibaba.fastjson.JSONArray jsonArray)
对象引用转换,内存指针依旧指向元数据
jsonArray - 需要转换的JSONArrayListUtils.toJsons(JSONArray)public static com.alibaba.fastjson.JSONObject[] toJsons(List<com.alibaba.fastjson.JSONObject> list)
对象引用转换,内存指针依旧指向元数据
list - 需要转换的ListListUtils.toJsons(List)public static <T> com.alibaba.fastjson.JSONObject[] toJsonsT(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonsT(List)public static <T> com.alibaba.fastjson.JSONObject[] toJsonsTAndRemoveEmpty(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonsTAndRemoveEmpty(List)public static com.alibaba.fastjson.JSONObject[] toJsons(String jsonString)
jsonString - 需要转换的JSON字符串ListUtils.toJsons(String)public static com.alibaba.fastjson.JSONObject[] toJsons(String text, String regex, String key)
示例:
String text = "1,3,5,9";
JSONObject[] jsons = toJsons(text, ",", "id");
System.out.println(Arrays.toString(jsons));
结果:
[{"id":"1"}, {"id":"3"}, {"id":"5"}, {"id":"9"}]
text - 需要转换的文本regex - 文本分割表达式,同String类的split()方法key - JSON的key名称ListUtils.toJsons(String, String, String)Copyright © 2020 Pivotal Software, Inc.. All rights reserved.