java 结构体转json_将json转换为数据结构体

本文介绍了一个使用json-lib库将JSON转换为Java数据结构体的方法。核心代码展示了如何递归解析JSON对象,并创建对应的NodeContainer列表。同时提供了一个测试用例,验证了转换的正确性。
摘要由CSDN通过智能技术生成

主要用到的依赖:(划重点:这个依赖需要加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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值