JSON映射字段。可映射成对象或数组

因为项目对接了jira,jira api查询的数据太复杂且没有固定格式,所以写了一个简单的映射

// 原始数据
{
    "expand": "projects",
    "projects": [
        {
            "self": "http://10.50.10.100:30089/rest/api/2/project/10003",
            "id": "10003",
            "key": "GO",
            "name": "GO",
            "avatarUrls": {
                "48x48": "http://10.50.10.100:30089/secure/projectavatar?pid=10003&avatarId=10606",
                "24x24": "http://10.50.10.100:30089/secure/projectavatar?size=small&pid=10003&avatarId=10606",
                "16x16": "http://10.50.10.100:30089/secure/projectavatar?size=xsmall&pid=10003&avatarId=10606",
                "32x32": "http://10.50.10.100:30089/secure/projectavatar?size=medium&pid=10003&avatarId=10606"
            },
            "issuetypes": [
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10002",
                    "id": "10002",
                    "description": "需要完成的任务。",
                    "iconUrl": "http://10.50.10.100:30089/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
                    "name": "任务",
                    "subtask": false
                },
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10003",
                    "id": "10003",
                    "description": "事务的子任务。",
                    "iconUrl": "http://10.50.10.100:30089/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype",
                    "name": "开发子任务",
                    "subtask": true
                },
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10001",
                    "id": "10001",
                    "description": "由 Jira Software 创建——请勿编辑或删除。适用于一种用户故事的事务类型。",
                    "iconUrl": "http://10.50.10.100:30089/images/icons/issuetypes/story.svg",
                    "name": "故事",
                    "subtask": false
                },
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10202",
                    "id": "10202",
                    "description": "For Go2Group SYNAPSE Test Case issue type",
                    "iconUrl": "http://10.50.10.100:30089/download/resources/com.go2group.jira.plugin.synapse/synapse/images/icon-testcase.png",
                    "name": "测试用例",
                    "subtask": false
                },
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10203",
                    "id": "10203",
                    "description": "For Go2Group SYNAPSE Test Plan issue type",
                    "iconUrl": "http://10.50.10.100:30089/download/resources/com.go2group.jira.plugin.synapse/synapse/images/icon-testplan.png",
                    "name": "测试计划",
                    "subtask": false
                },
                {
                    "self": "http://10.50.10.100:30089/rest/api/2/issuetype/10204",
                    "id": "10204",
                    "description": "For Go2Group SYNAPSE Bug issue type",
                    "iconUrl": "http://10.50.10.100:30089/download/resources/com.go2group.jira.plugin.synapse/synapse/images/icon-bug.png",
                    "name": "缺陷",
                    "subtask": false
                }
            ]
        }
    ]
}

我想取
projects.issuetypes.id和projects.issuetypes.name

// 解析后数据
[
  {
    "issueTypeId": "10002",
    "issueTypeName": "任务"
  },
  {
    "issueTypeId": "10003",
    "issueTypeName": "开发子任务"
  },
  {
    "issueTypeId": "10001",
    "issueTypeName": "故事"
  },
  {
    "issueTypeId": "10202",
    "issueTypeName": "测试用例"
  },
  {
    "issueTypeId": "10203",
    "issueTypeName": "测试计划"
  },
  {
    "issueTypeId": "10204",
    "issueTypeName": "缺陷"
  }
]

代码如下

/**
 * @author Ledison
 * @date 2022/2/28
 */
public class JsonAdapter extends AbstractAdapter implements AdapterExecute {

    public JsonAdapter(Map map) {
        super(map);
    }

    private static void parseObject(String data, List> out, String parentKey, Map map, Map entity) {
        JSONObject jsonObject = JSON.parseObject(data);
        for (Map.Entry entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof JSONArray) {
                if (Func.isNotBlank(parentKey)) {
                    parseArray(JSON.toJSONString(value), parentKey + "." + key, out, map);
                } else {
                    parseArray(JSON.toJSONString(value), key, out, map);
                }
            } else if (value instanceof JSONObject) {
                if (Func.isNotBlank(parentKey)) {
                    parseObject(JSON.toJSONString(value), out, parentKey + "." + key, map, entity);
                } else {
                    parseObject(JSON.toJSONString(value), out, key, map, entity);
                }
            } else {
                if (Func.isBlank(parentKey) && map.containsKey(key)) {
                    entity.put(map.get(key), value);
                } else if (Func.isNotBlank(parentKey) && map.containsKey(parentKey + "." + key)) {
                    entity.put(map.get(parentKey + "." + key), value);
                }
            }
        }
    }

    private static void parseArray(String data, String parentKey, List> out, Map map) {
        JSONArray ja = JSONArray.parseArray(data);
        for (int i = 0; i < ja.size(); i++) {
            Object foreachJa = ja.get(i);
            if (foreachJa instanceof JSONArray) {
                parseArray(JSON.toJSONString(foreachJa), parentKey, out, map);
            } else if (foreachJa instanceof JSONObject) {
                Map entity = new HashMap<>();
                out.add(entity);
                parseObject(JSON.toJSONString(foreachJa), out, parentKey, map, entity);
            }
        }
    }

    @Override
    public R> oneData(String data) {
        List> maps = allData(data);
        return Func.isNotEmpty(maps) ? R.data(maps.get(0)) : R.data(new HashMap<>());
    }

    public List> allData(String data) {
        Object parse = JSON.parse(data);
        List> out = new ArrayList<>();
        if (parse instanceof JSONArray) {
            parseArray(data, null, out, map);
        } else if (parse instanceof JSONObject) {
            Map entity = new HashMap<>();
            out.add(entity);
            parseObject(data, out, null, map, entity);
        }
        CopyOnWriteArrayList> list = new CopyOnWriteArrayList(out);
        list.removeIf(Func::isEmpty);
        return list;
    }

    @Override
    public R>> pageData(String data, Query query) {
        IPage> page = Condition.getPage(query);
        List> maps = allData(data);
        List> pageData = maps.stream().skip((page.getCurrent() - 1) * page.getSize()).limit(page.getSize()).collect(Collectors.toList());
        page.setRecords(pageData);
        page.setTotal(maps.size());
        return R.data(page);
    }
}

使用方式

Map map = new HashMap<>();
        // 字段映射
        map.put("projects.issuetypes.id","issueTypeId");
        map.put("projects.issuetypes.name","issueTypeName");
        JsonAdapter jsonAdapter = new JsonAdapter(map);
        // 原始数据
        String data = "";
        List> maps = jsonAdapter.allData(data);

你可能感兴趣的:(spring,boot,后端,java)