为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

12345678_0001 2023-10-19 21:31:40
我有多个 json 字符串,例如:JSON 1:{"a":"test1","b":"test2"}JSON 2:{"b":"test3","c":"test4"}我想要最终的 json 为:{"a":"test1","b":["test2","test3"],"c":"test4"}我怎样才能在java中做到这一点?
查看完整描述

3 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

您可以使用任何一种最流行的 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) {

    Map<String, String> jsonContent = mapper.readValue(jsonStr, Map.class);

    jsonContent.forEach((k,v) -> {

        if (jsonMap.containsKey(k)) {

            if (jsonMap.get(k) instanceof String) {

                List<String> content = new ArrayList<>();

                content.add(jsonMap.get(k).toString());

                content.add(v);

                jsonMap.put(k, content);

            } else {

                jsonMap.put(k, ((ArrayList) jsonMap.get(k)).add(v));

            }

        } else {

            jsonMap.put(k, v);

        }

    });

}

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"}


查看完整回答
反对 回复 2023-10-19
?
Qyouu

TA贡献1786条经验 获得超11个赞

您需要一个 API 或框架来在 java 中解析 JSON。然后,您必须迭代 JSON 字符串,将它们解析为键值对。一旦你有了它,我建议使用 Map 来按键存储它们。这是一些伪代码:


public class KeyValuePair {

    private String key = null;

    private String value = null;

    // todo create constructor with arguments

    // todo create getters and setters

}


private List<KeyValuePair> parseJSON(String json) {

    List<KeyValuePair> parsed = new ArrayList<>();

    // todo use the JSON API you chose to parse the json string into an ArrayList of KeyValuePair

    return parsed;

}


Map<String, List<String>> results = new HashMap<>();

List<String> jsonStrings = new ArrayList<>();

// todo read your JSON strings into jsonStrings

for (String jsonString : jsonStrings) {

    List<KeyValuePair> pairs = parseJSON(jsonString);

    for (KeyValuePair pair : pairs) {

        List<String> values = results.get(pair.getKey());

        if (values == null) {

            values = new ArrayList<>();

            results.put(pair.getKey(), values);

        }

        values.add(pair.getValue());

    }

}

// todo you'll have to loop through the map's keys and construct your result JSON


查看完整回答
反对 回复 2023-10-19
?
慕少森

TA贡献2019条经验 获得超9个赞

您可以使用JSONObject 类来执行您需要的操作。据我了解,您目前有一些字符串。您可以使用构造函数为每个字符串创建一个 JSONObject:

JSONObject jObject = new JSONObject(String str);

然后,您可以迭代 JSONObject,执行所有检查并在新 JSONObject 中构造新 JSON。您有一些非常好的方法对您非常有帮助,但我认为 get 和 put 方法足以实现此合并。


查看完整回答
反对 回复 2023-10-19
  • 3 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信