将多个字符串json合并到一个java中

我有多个 json 字符串,例如:


JSON 1:


{

"a":"test1",


"b":"test2"

}

JSON 2:


{

"b":"test3",


"c":"test4"

}

我想要最终的 json 为:


{

"a":"test1",


"b":["test2","test3"],

"c":"test4"

}

我怎样才能在java中做到这一点?


12345678_0001
浏览 169回答 3
3回答

慕哥9229398

您可以使用任何一种最流行的 JSON 库来实现这一目标,以下示例演示了如何将多个 JSON 字符串合并为一个Jackson。我使用Map<String, Object>(如jsonMap)作为合并的 JSON 字符串。如果所有给定 JSON 字符串中的键都相同,则jsonMap中的值将为String。否则,其值为List<String>。示例代码List<String> jsonStrList = Arrays.asList("{\"a\":\"test1\",\"b\":\"test2\"}","{\"b\":\"test3\",\"c\":\"test4\"}");ObjectMapper mapper = new ObjectMapper();Map<String, Object> jsonMap = new HashMap<>();for (String jsonStr : jsonStrList) {&nbsp; &nbsp; Map<String, String> jsonContent = mapper.readValue(jsonStr, Map.class);&nbsp; &nbsp; jsonContent.forEach((k,v) -> {&nbsp; &nbsp; &nbsp; &nbsp; if (jsonMap.containsKey(k)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (jsonMap.get(k) instanceof String) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> content = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content.add(jsonMap.get(k).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content.add(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonMap.put(k, content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonMap.put(k, ((ArrayList) jsonMap.get(k)).add(v));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonMap.put(k, v);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}System.out.println(jsonMap.toString());System.out.println(new ObjectMapper().writeValueAsString(jsonMap).toString());控制台输出{a=test1, b=[test2], c=test4}{"a":"test1","b":["test2","test3"],"c":"test4"}

Qyouu

您需要一个 API 或框架来在 java 中解析 JSON。然后,您必须迭代 JSON 字符串,将它们解析为键值对。一旦你有了它,我建议使用 Map 来按键存储它们。这是一些伪代码:public class KeyValuePair {&nbsp; &nbsp; private String key = null;&nbsp; &nbsp; private String value = null;&nbsp; &nbsp; // todo create constructor with arguments&nbsp; &nbsp; // todo create getters and setters}private List<KeyValuePair> parseJSON(String json) {&nbsp; &nbsp; List<KeyValuePair> parsed = new ArrayList<>();&nbsp; &nbsp; // todo use the JSON API you chose to parse the json string into an ArrayList of KeyValuePair&nbsp; &nbsp; return parsed;}Map<String, List<String>> results = new HashMap<>();List<String> jsonStrings = new ArrayList<>();// todo read your JSON strings into jsonStringsfor (String jsonString : jsonStrings) {&nbsp; &nbsp; List<KeyValuePair> pairs = parseJSON(jsonString);&nbsp; &nbsp; for (KeyValuePair pair : pairs) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> values = results.get(pair.getKey());&nbsp; &nbsp; &nbsp; &nbsp; if (values == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.put(pair.getKey(), values);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; values.add(pair.getValue());&nbsp; &nbsp; }}// todo you'll have to loop through the map's keys and construct your result JSON

慕少森

您可以使用JSONObject 类来执行您需要的操作。据我了解,您目前有一些字符串。您可以使用构造函数为每个字符串创建一个 JSONObject:JSONObject jObject = new JSONObject(String str);然后,您可以迭代 JSONObject,执行所有检查并在新 JSONObject 中构造新 JSON。您有一些非常好的方法对您非常有帮助,但我认为 get 和 put 方法足以实现此合并。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java