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

    我要被自己蠢哭了,新做了一台电脑装SQL sever为了存数据,配置完,突然间发现MSSQLSEVER 不能重新启动,相当于之前的配置,还IP都白弄了.我找原因找了3个小时,后来发现是手欠启动的不该启 ...

  2. 剑指Offer 29. 最小的K个数 (其他)

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 题目地址 https://www.nowcoder.com/prac ...

  3. PyCharm介绍与基础操作

    PyCharm是一种Python IDE(集成开发软件),带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具. 1.1 PyCharm主界面 PyCharm包含4个主要区分,分别为菜单 ...

  4. hdu4612 Warm up 缩点+树的直径

    题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...

  5. echarts移除百度地图logo方法

    移除百度地图LOGO和版权信息 在jsp头上加上如下代码即可: <style type="text/css"> .anchorBL{ display:none } &l ...

  6. CCF-模板生成系统-201509-3

    主要是string---STL的运用 趁机整理一下erase, find, substr, replace, insert #include <bits/stdc++.h> using n ...

  7. js中的Call()和apply()

    Call和apply,就是改变函数里面的this指向的方法. xxx.call()或xxx.apply() 特别强调下xxx必须是function,(普通函数,类,构造函数) var obj={ na ...

  8. PythonStudy——python中如何使输出不换行

    1.在python 3.x版本中,使用print(,end="") 可使输出不换行,  例如:

  9. fromkeys()

    #fromkeys() #说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值 #案例 d=[1,2,3] dict={} dict=dict.fromkeys( ...

  10. 虚拟化cpu

    vmware的虚拟机cpu [root@84-monitor ~]# lscpuArchitecture:          x86_64CPU op-mode(s):        32-bit, ...