From f203b4f49be0ffff41f3e47d77c6205c162cc6bf Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Thu, 27 Jan 2022 13:13:46 +0800 Subject: [PATCH] =?UTF-8?q?Json=E5=B7=A5=E5=85=B7=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/central/common/utils/JsonUtil.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/JsonUtil.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/JsonUtil.java index 56da985a..575dd56d 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/JsonUtil.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/JsonUtil.java @@ -88,15 +88,11 @@ public class JsonUtil { * @param json json字符串 * @param cls 目标对象 */ - public static T toObject(String json, Class cls) { - if(StringUtils.isBlank(json) || cls == null){ + public static T toObject(JsonNode json, Class cls) { + if(json == null || cls == null){ return null; } - try { - return MAPPER.readValue(json, cls); - } catch (IOException e) { - throw new RuntimeException(e); - } + return MAPPER.convertValue(json, cls); } /** @@ -134,6 +130,22 @@ public class JsonUtil { } } + /** + * 字符串转换为指定对象 + * @param json json字符串 + * @param cls 目标对象 + */ + public static T toObject(String json, Class cls) { + if(StringUtils.isBlank(json) || cls == null){ + return null; + } + try { + return MAPPER.readValue(json, cls); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /** * 字符串转换为JsonNode对象 * @param json json字符串