因为项目对接了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<String, String> map) {
super(map);
}
private static void parseObject(String data, List<Map<String, Object>> out, String parentKey, Map<String, String> map, Map<String, Object> entity) {
JSONObject jsonObject = JSON.parseObject(data);
for (Map.Entry<String, Object> 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<Map<String, Object>> out, Map<String, String> 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<String, Object> entity = new HashMap<>();
out.add(entity);
parseObject(JSON.toJSONString(foreachJa), out, parentKey, map, entity);
}
}
}
@Override
public R<Map<String, Object>> oneData(String data) {
List<Map<String, Object>> maps = allData(data);
return Func.isNotEmpty(maps) ? R.data(maps.get(0)) : R.data(new HashMap<>());
}
public List<Map<String, Object>> allData(String data) {
Object parse = JSON.parse(data);
List<Map<String, Object>> out = new ArrayList<>();
if (parse instanceof JSONArray) {
parseArray(data, null, out, map);
} else if (parse instanceof JSONObject) {
Map<String, Object> entity = new HashMap<>();
out.add(entity);
parseObject(data, out, null, map, entity);
}
CopyOnWriteArrayList<Map<String, Object>> list = new CopyOnWriteArrayList(out);
list.removeIf(Func::isEmpty);
return list;
}
@Override
public R<IPage<Map<String, Object>>> pageData(String data, Query query) {
IPage<Map<String, Object>> page = Condition.getPage(query);
List<Map<String, Object>> maps = allData(data);
List<Map<String, Object>> 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<String, String> map = new HashMap<>();
// 字段映射
map.put("projects.issuetypes.id","issueTypeId");
map.put("projects.issuetypes.name","issueTypeName");
JsonAdapter jsonAdapter = new JsonAdapter(map);
// 原始数据
String data = "";
List<Map<String, Object>> maps = jsonAdapter.allData(data);