主要用到的依赖:(划重点:这个依赖需要加jdk版本号,不加的话用不了,且目前最高是jdk15)
(ps: 用于json与其他类型格式转换,JSONObject, JSONArray等来自这个包)
net.sf.json-lib
json-lib
2.4
jdk15
核心代码:
packagecn.ucmed.pangu.lib;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importorg.apache.commons.collections.CollectionUtils;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;public classConvertTool {public static ListanalysisRequestJson(Object json) {
List baseNodeList = new ArrayList<>();
NodeContainer nodeContainer= new NodeContainer(null, null, null, null);if (json instanceofJSONArray) {
JSONArray jsonArray=(JSONArray) json;
jsonArray.forEach(j->analysisRequestJson(j));
}else if (json instanceofJSONObject) {
JSONObject jsonObject=(JSONObject) json;
Iterator iterator=jsonObject.keys();while(iterator.hasNext()) {
String key=iterator.next().toString();
Object o=((JSONObject) json).get(key);if (o instanceofJSONArray) {
JSONArray jsonArray=(JSONArray) o;
analysisRequestJson(jsonArray);
}else if (o instanceofJSONObject) {
List list =analysisRequestJson((JSONObject) o);
nodeContainer= new NodeContainer(key, null, list);
}else{
baseNodeList.add(new NodeLeafConstant(key, o.toString(), o.getClass().getTypeName().replace("java.lang.", "")));
}
}if(CollectionUtils.isNotEmpty(nodeContainer.nodeList)) {
baseNodeList.add(nodeContainer);
}
}returnbaseNodeList;
}
}
测试用例:
packagecn.ucmed.pangu.test;importcn.ucmed.pangu.lib.ConvertTool;importcn.ucmed.pangu.lib.NodeContainer;importcn.ucmed.pangu.lib.NodeLeafConstant;importnet.sf.json.JSONObject;importorg.junit.Assert;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.ArrayList;importjava.util.Arrays;
@RunWith(SpringRunner.class)public classConvertToolTest {public static String jsonStr = "{\n" +
" \"app_id\":\"zsyy_android\",\n" +
" \"app_key\":\"ZW5sNWVWOWhibVJ5YjJsaw==\",\n" +
" \"coder\":\"enNseVpXNXNOV1ZXT1doaWJWSjVZakpzYXc9PQ\",\n" +
" \"api_name\":\"api.nuts.invoker\",\n" +
" \"data\":{\n" +
" \"invoker_content\":{\n" +
" \"apiId\":\"QueryDeptSchema\",\n" +
" \"UseWay\":\"卓健\",\n" +
" \"TransCode\":\"2004\",\n" +
" \"UserId\":\"ZJYY\",\n" +
" \"DeptCode\":\"1014\",\n" +
" \"SeeDate\":\"2018-7-1\",\n" +
" \"EndDate\":\"2018-7-5\"\n" +
" }\n" +
" }\n" +
"}";public static NodeContainer expectedNodeContainer = new NodeContainer(new ArrayList<>(Arrays.asList(new NodeLeafConstant("app_id", "zsyy_android", "String"),new NodeLeafConstant("app_key", "ZW5sNWVWOWhibVJ5YjJsaw==", "String"),new NodeLeafConstant("coder", "enNseVpXNXNOV1ZXT1doaWJWSjVZakpzYXc9PQ", "String"),new NodeLeafConstant("api_name", "api.nuts.invoker", "String"),new NodeContainer("data", null, Arrays.asList(new NodeContainer("invoker_content", null, Arrays.asList(new NodeLeafConstant("apiId", "QueryDeptSchema", "String"),new NodeLeafConstant("UseWay", "卓健", "String"),new NodeLeafConstant("TransCode", "2004", "String"),new NodeLeafConstant("UserId", "ZJYY", "String"),new NodeLeafConstant("DeptCode", "1014", "String"),new NodeLeafConstant("SeeDate", "2018-7-1", "String"),new NodeLeafConstant("EndDate", "2018-7-5", "String")
))
))
)));
@Testpublic voidtest() {
JSONObject jsonObject=JSONObject.fromObject(jsonStr);
NodeContainer requestNode= new NodeContainer(new ArrayList<>(ConvertTool.analysisRequestJson(jsonObject)));
Assert.assertEquals(requestNode.toString(), expectedNodeContainer.toString());
}
}
原文:https://www.cnblogs.com/miaoying/p/10045675.html