我有两个json文件。我需要解析并将json加入到单个结构中
lineage.json
{
"lineage": [{
"sourceColumnId": "VMB_BESTADDRESS.SNAPSHOT_TS",
"description": "",
"targetColumnId": "VMB_BESTADDRESSUSAGE.NXREINS"
},
{
"sourceColumnId": "DSL_RECORD_SOURCES.MAMACT",
"description": "",
"targetColumnId": "G2_ZUMADF00.MAMACT"
},
{
"sourceColumnId": "DSL_RECORD_SOURCES.MAMADE",
"description": "",
"targetColumnId": "G2_ZUMADF00.HDF_S_POL_GEN"
}]
}
column.json
{
"column": [{
"ID": 39700,
"columnId": "VMB_BESTADDRESS.SNAPSHOT_TS",
"column": "SNAPSHOT_TS",
"dataType": "String",
"length": "",
"table": "VMB_BESTADDRESS",
},
{
"ID": 39701,
"columnId": "VMB_BESTADDRESSUSAGE.NXREINS",
"column": "NXREINS",
"dataType": "String",
"length": "",
"table": "VMB_BESTADDRESSUSAGE",
},
{
"ID": 39702,
"columnId": "VMB_BESTADDRESSUSAGE.PKADDRESSCODE",
"column": "PKADDRESSCODE",
"dataType": "String",
"length": "",
"table": "VMB_BESTADDRESSUSAGE",
}]
}
我需要加入两个jsons,以便对于列中的sourcecolumnId和targetcolumnid的每个匹配,必须填充以下json结构
{
output:{
sourceColumnId:VMB_BESTADDRESS.SNAPSHOT_TS,
sourceColumnName:SNAPSHOT_TS,
targetColumnId:VMB_BESTADDRESSUSAGE.NXREINS,
targetColumnNameNXREINS,
}
}
我需要连接查找两个json以获得输出
sourceColumnName - > column.json的列名,其columnId和sourceColumnId相同。 类似地,对于targetcolumnName也是。
答案 0 :(得分:0)
String str = "xxx"; // column.json
JSONArray jsonArrayColumn = JSON.parseObject(str);
Map<String, JSONObject> column = new HashMap(); // {id:name}
for(int i = 0; i < jsonArrayColumn.size(); i++){ // loop
JSONObject obj = jsonArrayColumn.getJSONObject(i);
column.put(obj.getString("columnId"), obj.getString("column"));
}
String str2 = "xxx"; // lineage.json
JSONArray jsonArrayLineage = JSON.parseObject(str2);
JSONArray resultArray = new JSONArray();
for(int i = 0; i < jsonArrayLineage.size(); i++){ // loop
JSONObject lineageObj = jsonArrayLineage.getJSONObject(i);
JSONObject obj = new JSONObject();
obj.put("sourceColumnId", lineageObj.get("sourceColumnId"));
obj.put("sourceColumnName", column.get(lineageObj.get("sourceColumnId")));
obj.put("targetColumnId", lineageObj.get("targetColumnId"));
obj.put("targetColumnName", column.get(lineageObj.get("targetColumnId")));
}
resultArray
就是你想要的。