赞
踩
package com.cwq.demo02; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** * @author Carpoor * @date 2021年5月19日 */ public class JsonUtil { public static Set<String> set = new HashSet<>(); static { set.add("String"); set.add("Integer"); set.add("Double"); } public static <T>T str2Obj(String json, Class<T> clazz) { Gson gson = new GsonBuilder() .disableHtmlEscaping() .serializeNulls() .create(); return gson.fromJson(json, clazz); } public static String obj2Str(Object obj) { Gson gson = new GsonBuilder() .disableHtmlEscaping() .serializeNulls() .create(); return gson.toJson(obj); } public static Map jsonCompare(String currentJson, String expectJson) { Map<String, String> result = new HashMap<>(); Map<String, Object> curr = str2Obj(currentJson, Map.class); Map<String, Object> exp = str2Obj(expectJson, Map.class); mapProc(curr, exp, result, ""); return result; } public static void mapProc(Map<String, Object> curr, Map<String, Object> exp, Map<String, String> result, String keyStr) { if (curr.size() != exp.size()) { result.put(keyStr == "" ? "首层":keyStr, String.format("key=[%s]Map字段个数不一致,期望为[%s],但当前为[%s]", keyStr == "" ? "首层":keyStr, exp.size(), curr.size())); } for (String currKey : curr.keySet()) { String keyStrCopy = keyStr + "."+ currKey; if (keyStrCopy.indexOf(".") == 0) { keyStrCopy = keyStrCopy.substring(1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。