zoukankan      html  css  js  c++  java
  • 将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 = "{
    " +
                "    "app_id":"zsyy_android",
    " +
                "    "app_key":"ZW5sNWVWOWhibVJ5YjJsaw==",
    " +
                "    "coder":"enNseVpXNXNOV1ZXT1doaWJWSjVZakpzYXc9PQ",
    " +
                "    "api_name":"api.nuts.invoker",
    " +
                "    "data":{
    " +
                "        "invoker_content":{
    " +
                "            "apiId":"QueryDeptSchema",
    " +
                "            "UseWay":"卓健",
    " +
                "            "TransCode":"2004",
    " +
                "            "UserId":"ZJYY",
    " +
                "            "DeptCode":"1014",
    " +
                "            "SeeDate":"2018-7-1",
    " +
                "            "EndDate":"2018-7-5"
    " +
                "        }
    " +
                "    }
    " +
                "}";
    
        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());
        }
    }

  • 相关阅读:
    vue如何实现热更新
    vue项目与node项目分离
    vue如何实现热更新
    Vue项目SSR改造实战
    找工作Java面试 题搜集
    html5绘图笔记纪要
    HTML5实现绘制几何图形
    浅谈html5在vr中的应用
    浏览器三种事件处理的模型
    基于html5二个div 连线
  • 原文地址:https://www.cnblogs.com/miaoying/p/10045675.html
Copyright © 2011-2022 走看看