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

    解释器 运行步骤: 找到环境变量CLASSPATH,CLASSPATH包含一个或者多个目录,用作查找.class文件的根目录 从根目录开始,解释器获取包名并将每个.替换成\或/(取决于操作系统) 得到 ...

  2. SQL语句中Left join,right join,inner join用法

    转载于:https://blog.csdn.net/lichkui/article/details/2002895 一.先看一些最简单的例子 例子 Table Aaid   adate 1      ...

  3. day38-多进程多线程-进程池

    强大的Manage上一篇的数据共享的方式只有两种结构Value和Array.Python中提供了强大的Manage专门用来做数据共享的,其支持的类型非常多,包括,Value, Array,list,d ...

  4. tensorFlow 零散知识

    收集一些碰到的关于细节的函数在这里记录下 1.tf.flags.DEFINE_xxx() 读别人家的代码的时候经常看到这个,结果两三天不看居然忘记了,这脑子绝对上锈了,决定记下来免得老是查来查去的.. ...

  5. SQL-45 将titles_test表名修改为titles_2017。

    题目描述 将titles_test表名修改为titles_2017.CREATE TABLE IF NOT EXISTS titles_test (id int(11) not null primar ...

  6. Unity3d外部加载音频,视频,图片等资源 及根据路径获取制定格式的文件

    1.根据路径获取制定文件类型的文件: 这里写一个类,调用了打开路径的方法:using UnityEngine;using System;using System.Collections.Generic ...

  7. guava-retrying 源码解析(时间限制策略)

    一.时间限制策略相关接口和类 什么是时间限制策略呢?是指在一个时间限制内,包装任何一种重试(尝试)规则,如果超过该限制,那么这个尝试规则可能会被中断,并抛出UncheckedTimeoutExcept ...

  8. linux之目录文件操作

  9. Element分页组件prev-text和next-text属性无效?

    前情提要 /(ㄒoㄒ)/~~ 作为刚刚接触 Element 组件的人来说,看文档是第一步,但是当我想要修改分页组件里面的按钮时却遇到了问题. 文档中写到是需要给 prev-text 和 next-te ...

  10. 用rz、sz命令在Xshell传输文件

    用rz.sz命令在Xshell传输文件 2014-03-27 14:38:17 标签:用rz.sz命令在Xshell传输文件 Xshell很好用,然后有时候想在windows和linux之间上传或下载 ...