主要用到的依赖:(划重点:这个依赖需要加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转换为数据结构体的更多相关文章

  1. json转换为键值对辅助类

    /// <summary> /// json转换为键值对辅助类 /// </summary> public class JsonParser { private static ...

  2. 字符串json转换为xml xml转换json

    原文:字符串json转换为xml xml转换json // To convert an XML node contained in string xml into a JSON string XmlD ...

  3. 关于JS 的cookie 操作 与 json 的数据结构 问题

    今天写了一个购物车,由于购物车内容是保存在 cookie中 所以不想浪费服务器资源做cookie的操作 故在前端封装了一些对象来处理购物车,由于cookie的数据结构的设计是一个json格式 使用 账 ...

  4. Visual Studio 2015 将json转换为实体类

    最新写的一个接口需要接收json参数,然后序列化为实体类然后再进行后面的逻辑处理.因为json中键值对比较多,逐一去手写实体中的每个属性太麻烦,于是寻思是否有这样的工具可以将json转换为实体类. 经 ...

  5. c# 将json转换为DataTable

    /// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...

  6. json转换为map

    // json转换为map public static Map parserToMap(String s) { Map map = new HashMap(); JSONObject json = J ...

  7. c#常用的Datable转换为json,以及json转换为DataTable操作方法

    #region  DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  8. JSON的转换(将JSON转换为字符串,将字符串转化为JSON)

    eval()和JSON.stringify()   1.eval用于从一个字符串中解析出json 对象,创建包含 JSON 语法的 JavaScript 字符串.例如 var str = '{ &qu ...

  9. HttpPost请求将json作为请求体传入的简单处理方法

    https://www.cnblogs.com/mambahyw/p/7875142.html **************************************************** ...

随机推荐

  1. Android7.0手机程序保活(附源码下载)

    项目地址,求starhttps://github.com/979451341/AppAlive 本人在学习音视频时无意发现的黑科技,发现在使用AudioTrack播放音乐时,使用手机的一键清理发现程序 ...

  2. Beta 冲刺 (6/7)

    Beta 冲刺 (6/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 组织会议 开发wxpy部分功能 展示G ...

  3. FC磁盘设备管理

    本文转自:http://www.cactifans.org/linux/1071.html 转自EMC中文支持论坛 – https://community.emc.com/go/chinese   介 ...

  4. 入坑deep learning 1

    想体验一下跑keras的感觉,按照这个小妹妹的教程:https://zhuanlan.zhihu.com/p/28333410 0. 大概花了十来个小时才搞定初步的小环境 1. 在linux 16.0 ...

  5. 【Cocos2d-html5】运动中速度效果

    在我们使用Action系统动作的时候,比如MoveTo,在进行运动的时候总是在规定的时间内进行匀速运动,有时候可能会想添加一些加速度的效果,cocos2d-html5就依然和cocos2dx一样为我们 ...

  6. 最简单的操作 jetty IDEA 【debug】热加载

    [博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] http ...

  7. Java 内存模型简单剖析

    Java 内存模型试图屏蔽各种硬件和操作系统的内存访问差异,以实现让 Java 程序在各种平台下都能达到一致的内存访问效果. 主内存与工作内存 处理器上的寄存器的读写的速度比内存快几个数量级,为了解决 ...

  8. 前端修炼の道 | <div> 标签简介

    <div> 标签是最基本的,同时也是最常用的标签. 该标签是一个双标签,出现在主体区域中,主要作为一个容器标签来使用,在 <div> 标签中可以包含除 <body> ...

  9. 使用map()的小陷阱:parseInt

    假设我们想要把一个字符串数组的每一项转换成整数,我们很自然就想到了把parseInt作为回调函数传给map()函数,但这样做可能会出现意想不到的结果: var strArr = ["1&qu ...

  10. tomcat升级 遇到的坑

    今天说说tomcat升级后出的问题 以前的版本是8.0.30的 因用安全漏洞 需要升级tomcat 为8.5.28的版本 升级后jvm的配置 等等都和一起一样,过了几天发现,我们的错误日志和处理影响转 ...