将json转换为数据结构体
主要用到的依赖:(划重点:这个依赖需要加jdk版本号,不加的话用不了,且目前最高是jdk15)
(ps: 用于json与其他类型格式转换,JSONObject, JSONArray等来自这个包)
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
核心代码:
package cn.ucmed.pangu.lib; import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class ConvertTool { public static List<BaseNode> analysisRequestJson(Object json) {
List<BaseNode> baseNodeList = new ArrayList<>();
NodeContainer nodeContainer = new NodeContainer(null, null, null, null);
if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
jsonArray.forEach(j -> analysisRequestJson(j));
} else if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
Iterator iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next().toString();
Object o = ((JSONObject) json).get(key);
if (o instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) o;
analysisRequestJson(jsonArray);
} else if (o instanceof JSONObject) {
List<BaseNode> 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);
}
}
return baseNodeList;
}
}
测试用例:
package cn.ucmed.pangu.test; import cn.ucmed.pangu.lib.ConvertTool;
import cn.ucmed.pangu.lib.NodeContainer;
import cn.ucmed.pangu.lib.NodeLeafConstant;
import net.sf.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList;
import java.util.Arrays; @RunWith(SpringRunner.class)
public class ConvertToolTest { 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")
))
))
))); @Test
public void test() {
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
NodeContainer requestNode = new NodeContainer(new ArrayList<>(ConvertTool.analysisRequestJson(jsonObject)));
Assert.assertEquals(requestNode.toString(), expectedNodeContainer.toString());
}
}
将json转换为数据结构体的更多相关文章
- json转换为键值对辅助类
/// <summary> /// json转换为键值对辅助类 /// </summary> public class JsonParser { private static ...
- 字符串json转换为xml xml转换json
原文:字符串json转换为xml xml转换json // To convert an XML node contained in string xml into a JSON string XmlD ...
- 关于JS 的cookie 操作 与 json 的数据结构 问题
今天写了一个购物车,由于购物车内容是保存在 cookie中 所以不想浪费服务器资源做cookie的操作 故在前端封装了一些对象来处理购物车,由于cookie的数据结构的设计是一个json格式 使用 账 ...
- Visual Studio 2015 将json转换为实体类
最新写的一个接口需要接收json参数,然后序列化为实体类然后再进行后面的逻辑处理.因为json中键值对比较多,逐一去手写实体中的每个属性太麻烦,于是寻思是否有这样的工具可以将json转换为实体类. 经 ...
- c# 将json转换为DataTable
/// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...
- json转换为map
// json转换为map public static Map parserToMap(String s) { Map map = new HashMap(); JSONObject json = J ...
- c#常用的Datable转换为json,以及json转换为DataTable操作方法
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- JSON的转换(将JSON转换为字符串,将字符串转化为JSON)
eval()和JSON.stringify() 1.eval用于从一个字符串中解析出json 对象,创建包含 JSON 语法的 JavaScript 字符串.例如 var str = '{ &qu ...
- HttpPost请求将json作为请求体传入的简单处理方法
https://www.cnblogs.com/mambahyw/p/7875142.html **************************************************** ...
随机推荐
- vuex-getter
store 的计算属性. getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算 const store = new Vuex.Store({ state: { t ...
- <a></a>标签传参出现乱码问题
在段代码在传递参数的时候会出现中文乱码,正常情况下,只要在接收参数的时候写上: request.setCharacterEncoding("UTF-8");就能解决问题. 但是,今 ...
- 用matlab生成mif文件
在FPGA中,rom模块的初始化文件分为两种,一种是hex文件,另外一种是mif文件,这两种文件都可以通过Quartus进行手工创建,进行手工输入数据,也可以借助于专用的文件编辑器完成编辑. 在此介绍 ...
- mybatis oracle:批量操作(增删改查)
此文主要是讲mybatis在连接oracle数据库时的一些批量操作,请各位对号入座 (最后回来补充一下,所有都是在Spring+MVC的框架下实现的) 不废话,上代码: 1.批量插入(网上很多,是针对 ...
- Java基础-访问修饰符
访问修饰符 default (即缺省,什么也不写): 在同一包内可见,不使用任何修饰符.使用对象:类.接口.变量.方法. private : 在同一类内可见.使用对象:变量.方法. 注意:不能修饰类( ...
- Ajax实战(原生)
/** 之前一直在学习ajax,也拿jQuery写了很多,感觉还是写原生的对基础的理论理解的深刻.特此书写. * 得到ajax对象 */ function getajaxHttp() { var xm ...
- web 页面上纯js实现按钮倒计数功能(实时计时器也可以)
需求构思:本功能想实现的是,一个按钮在页面载入就显示提醒续费,,,倒数60秒后,完成提醒功能,可以按另外一个页面跳转到主页. 参考网上的大神,实现如下:Button2倒数,Button3跳转,在页面上 ...
- iframe子页面控制父页面滚动高度,直接蹦到父页面开头
zepto调用父页面窗口元素的scrollTop()方法会报错,貌似是scrollTop函数中有个scrollTo()方法用到this,指向错误. 经检查, 原生js控制父页面滚动,只能写数字,不能带 ...
- Hadoop学习笔记01_Hadoop搭建
想往大数据方向转, 难度肯定是有的. 基础知识肯定是要有的,如果是熟悉JAVA开发的人,转向应该优势大. 像我这样的,只有Linux基础以及简单的PHP基础的人,转向难度很大.但是事在人为,努力学习多 ...
- lettuce行为驱动框架实例
练习: 一:e1_MyFirstBDD 使用方法的方式实现阶乘的计算 zero.feature: Feature: Compute factorial In order to play with Le ...