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());
        }
    }

  • 相关阅读:
    [CQOI2005]三角形面积并(计算几何+扫描线)
    第一天
    LA3026 周期 (kmp)
    HDU 1715 大菲波数 (java大数)
    根据身高重建队列(vector)
    K 连续位的最小翻转次数
    724. Find Pivot Index
    Two Sum
    c
    Most Powerful(状压DP水题)
  • 原文地址:https://www.cnblogs.com/miaoying/p/10045675.html
Copyright © 2011-2022 走看看