将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 **************************************************** ...
随机推荐
- 1159 Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 68562 Accepted: 23869 Desc ...
- GANs的回顾
GANs第一篇比较熟悉,不介绍.看看变种. 1) LAPGANs:金字塔,逐步优化生成图片(Deep Generative Image Models using Lapalacian Pyramid ...
- HTML知识点梳理1
1,HTML基本结构 <!DOCTYPE html> <html> <head></head> <body> </body> & ...
- Arcgis属性表出现乱码
解决方案一:导入符号化字体: 在C:\Windows\Fonts文件夹下放入.TTF格式的字体库(此时加入农村二调_0.TTF和TDT10142007.ttf),便可使符号化的乱码显示正常. 解决方案 ...
- 12.python-metaclass元类
1.python中一切皆是对象,类本身也是一个对象,当使用关键字class的时候,python解释器在加载class的时候会创建一个对象(这里的对象指的是类而非类的实例) class Foo: #cl ...
- arch 安装准备--包管理的使用pacman
-------https://wiki.archlinux.org/index.php/Pacman/Tips_and_tricks#List_of_installed_packageshttps:/ ...
- hello2 源码解析
在hello2的项目中,采用的是Java servlet 技术来采取对项目的整体框架的搭建.编写另一个greeting的java文件,实现了一个greeting的java类来覆盖url的doGet方 ...
- Spring Boot 非常好的学习资料
from@https://gitee.com/didispace/SpringBoot-Learning Spring Boot 2.0 新特性学习 简介与概览 Spring Boot 2.0 正式发 ...
- 【BZOJ2555】SubString
算是学会sam了吧…… 原题: 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了 ...
- iso系统镜像刻录到光盘和U盘
使用UltraISO刻录 刻录U盘,点击文件,打开,选择镜像 启动,写入硬盘镜像选择U盘即可 刻录光盘 工具,刻录光盘映像,选择镜像,需要先插入光盘刻录机(有些电脑可能自带光驱盘,且有刻录功能,那么我 ...