treeMap实现分组数据树形结构
package com.xdz.utils;
import java.util.List;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONPath;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdz.domain.SourceItem;
public class JsonUtils {
public static final ObjectMapper mapper = new ObjectMapper();
private JsonUtils() {
}
/**
* 对象转json字符串
*
* @param obj
* @return
*/
public static String toJson(Object obj) {
try {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.writeValueAsString(obj);
} catch (Exception e) {
throw new BusinessException(e);
}
}
/**
* json字符串转对象
*
* @param json
* @param clazz
* @return
*/
public static <T> T fromJson(String json, Class<T> clazz) {
try {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(json, clazz);
} catch (Exception e) {
throw new BusinessException(e);
}
}
/**
* json字符串转对象
*
* @param json
* @param typeReference
* @return
*/
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
try {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(json, typeReference);
} catch (Exception e) {
throw new BusinessException(e);
}
}
public static <T> List<T> getArrayObject(String json, String path, Class<T> clz) {
String content = JSONPath.eval(json, path).toString();
if (!StringUtils.hasLength(content)) {
return null;
}
return JsonUtils.fromJson(content, new TypeReference<List<T>>() {
});
}
/**
* 获取路径一般属性
*
* @param json
* @param path
* @return
*/
public static Object getValueFromObj(String json, String path) {
return JSONPath.read(json, path);
}
public static void main(String[] args) {
String json = "{\"data\":{\"formInfo\":{\"widgetMap\":{\"_S_SERIAL\":{\"codeId\":\"_S_SERIAL\",\"title\":\"流水号\",\"type\":\"serialNumWidget\",\"value\":\"GYS/KHXXWHLC-20220518-010\"},\"_S_DATE\":{\"codeId\":\"_S_DATE\",\"fromNowOn\":false,\"title\":\"申请日期\",\"type\":\"dateWidget\",\"value\":1652850877047},\"_S_APPLY\":{\"eid\":\"17109746\",\"codeId\":\"_S_APPLY\",\"assignRule\":[],\"extendFieldMap\":{},\"existEcosphere\":false,\"optSource\":\"org\",\"title\":\"提交人\",\"type\":\"personSelectWidget\",\"personInfo\":[{\"image\":\"https://static.yunzhijia.com/space/c/photo/load?id=6212ecd41d562b00014f41e0\",\"companyName\":\"新大正物业集团\",\"jobTitle\":\"资料员\",\"name\":\"吴丹丹\",\"deptLongName\":\"新大正物业集团/重庆高筹智能工程有限公司/机电部\",\"oid\":\"621312a0e4b0030a90f3093a\",\"dept\":\"机电部\",\"userId\":\"6212ecd4e4b038416df88287\"}],\"value\":[\"621312a0e4b0030a90f3093a\"],\"option\":\"single\"},\"At_0\":{\"codeId\":\"At_0\",\"extendFieldMap\":{\"docOnly\":false,\"pdfOnly\":false},\"maximum\":200,\"title\":\"附件\",\"type\":\"attachmentWidget\",\"value\":[{\"fileName\":\"微信图片_20220518133444.jpg\",\"fileSize\":\"369446\",\"pdfFileSize\":\"null\",\"fileExt\":\"jpg\",\"fileType\":\"image/jpeg\",\"fileId\":\"6284858af5e38d00015f4129\"}]},\"_S_DEPT\":{\"eid\":\"17109746\",\"codeId\":\"_S_DEPT\",\"title\":\"所属部门\",\"type\":\"departmentSelectWidget\",\"deptInfo\":[{\"name\":\"机电部\",\"orgId\":\"cb36fcc0-5c43-4252-91e0-12aae19ed2f5\",\"realLongName\":\"新大正物业集团!重庆高筹智能工程有限公司!机电部\",\"longName\":\"机电部(重庆高筹智能工程有限公司)\"}],\"selectCompanyOnly\":false,\"value\":[\"cb36fcc0-5c43-4252-91e0-12aae19ed2f5\"]},\"_S_TITLE\":{\"codeId\":\"_S_TITLE\",\"extendFieldMap\":{\"titleEntity\":{\"kind\":\"TITLE_DEFAULT\",\"list\":[{\"formItem\":\"_S_APPLY\",\"kind\":\"ITEM_FORM_ITEM\"},{\"formItem\":\"的\",\"kind\":\"ITEM_STRING\"},{\"formItem\":\"模板名称\",\"kind\":\"ITEM_TEMPLATENAME\"}]},\"defaultTitle\":true},\"title\":\"标题\",\"type\":\"textWidget\",\"value\":\"重庆得乐机电设备有限公司供应商信息维护流程\"}},\"detailMap\":{\"Dd_0\":{\"buttonName\":\"添加明细\",\"codeId\":\"Dd_0\",\"openErpLink\":false,\"widgetVos\":{\"Te_5\":{\"codeId\":\"Te_5\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"具体开户行名称\",\"type\":\"textWidget\"},\"Te_4\":{\"codeId\":\"Te_4\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"备注\",\"type\":\"textWidget\"},\"Te_2\":{\"codeId\":\"Te_2\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"账户名称\",\"type\":\"textWidget\"},\"Te_1\":{\"codeId\":\"Te_1\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"供应商名称\",\"type\":\"textWidget\"},\"Te_7\":{\"codeId\":\"Te_7\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"银行卡号\",\"type\":\"textWidget\"},\"Te_6\":{\"codeId\":\"Te_6\",\"extendFieldMap\":{\"wordLimit\":200},\"title\":\"联行号\",\"type\":\"textWidget\"}},\"extendFieldMap\":{\"showSerialNum\":true},\"widgetValue\":[{\"Te_5\":\"中国光大银行重庆两江金州支行\",\"Te_4\":\"\",\"Te_2\":\"重庆得乐机电设备有限公司\",\"Te_1\":\"重庆得乐机电设备有限公司\",\"_id_\":\"1\",\"Te_7\":\"39510188000034377\",\"Te_6\":\"303653000179\"}],\"imageInfo\":{},\"title\":\"供应商信息\",\"type\":\"detailedWidget\"}}},\"basicInfo\":{\"nodeName\":null,\"eid\":\"17109746\",\"formDefId\":\"624e8beed51fac0001e0a82f\",\"dataType\":1,\"myNetworkInfo\":{\"eid\":\"17109746\",\"name\":\"新大正物业集团\"},\"title\":\"供应商信息维护流程\",\"nodeType\":null,\"formCodeId\":\"7f8a15b79d254200beafcb0d4584db3b\",\"actionType\":null,\"myPersonInfo\":{\"image\":\"https://static.yunzhijia.com/space/c/photo/load?id=6212ecd41d562b00014f41e0\",\"name\":\"吴丹丹\",\"oid\":\"621312a0e4b0030a90f3093a\"},\"formInstId\":\"628485b7561c39000116efef\",\"eventTime\":1652863389266,\"myDeptInfo\":{\"name\":\"机电部\",\"orgId\":\"cb36fcc0-5c43-4252-91e0-12aae19ed2f5\"},\"interfaceId\":null,\"interfaceName\":null,\"nodeId\":null}},\"error\":null,\"errorCode\":0,\"success\":true}";
String path = "$.data.formInfo.detailMap.Dd_0.widgetValue";
String content = JSONPath.eval(json, path).toString();
JSONArray array = JSONArray.parseArray(content);
// System.out.println(getPartJson(json,path));
}
}