1. 程式人生 > >將json轉換為資料結構體

將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 = "{\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());
    }
}