试试下面的例子。我相信这可以满足您合并两个列表、更新任何现有值并最终添加任何不在第一个查询中的值的要求。
另外,我考虑到您使用的是 Java Runtime Environment 7(JRE 1.7)。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class App {
private static String path1 = "C:\\path\\to\\AS400.json";
private static String path2 = "C:\\path\\to\\SQL_Server_query.json";
public static void main( String[] args ) {
JSONArray trailersAS400 = null;
JSONArray trailersSQLServer = null;
try {
FileReader fr = new FileReader(path1);
FileReader fr2 = new FileReader(path2);
// utilize simple json parser to parse json objects
trailersAS400 = (JSONArray) new JSONParser().parse(fr);
trailersSQLServer = (JSONArray) new JSONParser().parse(fr2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
List<Object> list = new ArrayList<Object>(); // store parsed JSON's data in a list
for (Object trSQL : trailersAS400) {
list.add(trSQL);
}
for (Object tr400 : trailersSQLServer) {
list.add(tr400);
}
HashMap<String, Object> map1 = new HashMap<String, Object>(); // maps truck L122 key value pairs
HashMap<String, Object> map2 = new HashMap<String, Object>(); // maps truck SQ46 key value pairs
for (Iterator iter = list.iterator(); iter.hasNext();) { // iterate through list and update map1 with key value pair.
HashMap<String, Object> truck = (HashMap<String, Object>) iter.next();
if (truck.containsValue("L122")) {
Set<Entry<String, Object>> s = truck.entrySet();
for (Entry<String, Object> entry : s) {
map1.put(entry.getKey(), entry.getValue());
}
} else if (truck.containsValue("SQ46")) { // iterate through list and update map2 with key value pair.
Set<Entry<String, Object>> s = truck.entrySet();
for (Entry<String, Object> entry : s) {
map2.put(entry.getKey(), entry.getValue());
}
}
}
// Update map with map1 & map2. Note the key is represented as the truck number.
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("L122", map1);
map.put("SQ46", map2);
Gson pp = new GsonBuilder().setPrettyPrinting().create();
String p = pp.toJson(map);
System.out.println(p);
}
}
输出:
{
"L122": {
"truckNumber": "L122",
"dedicated": false,
"sealed": false,
"created": {
"when": 1546498401156
},
"latitude": 0,
"type": "OV",
"loaded": false,
"intermodal": false,
"companyOwned": true,
"assetId": 308,
"customerId": "KTPH",
"onSite": false,
"modified": {
"when": 1546498401156
},
"trailerGroup": "C",
"id": 308,
"longitude": 0
},
"SQ46": {
"truckNumber": "SQ46",
"dedicated": false,
"sealed": false,
"created": {
"when": 1546498401156
},
"latitude": 0,
"type": "PO",
"loaded": false,
"intermodal": false,
"companyOwned": true,
"assetId": 309,
"customerId": "KTPH",
"onSite": true,
"modified": {
"when": 1546498401156
},
"trailerGroup": "C",
"id": 309,
"longitude": 0
}
}
出于性能的目的。您可以考虑使用 LinkedHashMap 与 HashMap,如上例所示。使用 LinkedHashMap 时迭代效率更高。下面的示例使用这种方法。
public class App {
private static String path1 = "C:\\path\\to\\AS400.json";
private static String path2 = "C:\\path\\to\\SQL_Server_query.json";
public static void main( String[] args ) {
JSONArray trailersAS400 = null;
JSONArray trailersSQLServer = null;
try {
FileReader fr = new FileReader(path1);
FileReader fr2 = new FileReader(path2);
// utilize simple json parser to parse json objects
trailersAS400 = (JSONArray) new JSONParser().parse(fr);
trailersSQLServer = (JSONArray) new JSONParser().parse(fr2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
List<Object> list = new ArrayList<Object>(); // store parsed JSON's data in a list
for (Object trSQL : trailersAS400) {
list.add(trSQL);
}
for (Object tr400 : trailersSQLServer) {
list.add(tr400);
}
HashMap<String, Object> map1 = new HashMap<String, Object>(); // maps truck L122 key value pairs
HashMap<String, Object> map2 = new HashMap<String, Object>(); // maps truck SQ46 key value pairs
Iterator<Object> iter = list.iterator();
while (iter.hasNext()) { // iterate through list and update map1 with key value pair.
LinkedHashMap<String, Object> truck = new LinkedHashMap<String, Object>();
truck.putAll((Map<? extends String, ? extends Object>) iter.next());
if (truck.containsValue("L122")) {
Set<Entry<String, Object>> s = truck.entrySet();
for (Entry<String, Object> entry : s) {
map1.put(entry.getKey(), entry.getValue());
}
} else if (truck.containsValue("SQ46")) { // iterate through list and update map2 with key value pair.
Set<Entry<String, Object>> s = truck.entrySet();
for (Entry<String, Object> entry : s) {
map2.put(entry.getKey(), entry.getValue());
}
}
}
// Update map with map1 & map2. Note the key is represented as the truck number.
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("L122", map1);
map.put("SQ46", map2);
Gson pp = new GsonBuilder().setPrettyPrinting().create();
String p = pp.toJson(map);
System.out.println(p);
}
}
两个示例代码都将产生您想要的结果,如上述输出所示。