简体   繁体   English

如何正确转换 HashMap <string,list<object> &gt; 到 Json 阵列 json object </string,list<object>

[英]How to properly convert HashMap<String,List<Object>> to Json with array of json object

I have a stream of Kafka messages and wanted to build a HashMap<String,List<Object>> to be used as API response in Json format.我有一个 Kafka 消息的 stream 并且想要构建一个HashMap<String,List<Object>>以用作 Json 格式的 API 响应。

for (ConsumerRecord<String,String> consumerRecord : records) {
    if(!responses.containsKey(consumerRecord.topic())){
        responses.put(consumerRecord.topic(), new ArrayList<Object>());
    }
    responses.get(consumerRecord.topic()).add(JSONObject.stringToValue(consumerRecord.value()));
}

expected response:预期反应:

{
    "topic1": [
        {"field1":"value1","field2":"value2","field3":"value3"}
    ],
    "topic2": [
        {"field1":"value1","field2":"value2","field3":"value3"},
        {"anotherfield1":"anothervalue1","anotherfield2":"anothervalue2"}
    ]
}

actual response:实际反应:

{
    "topic1": [
        "{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}"
    ],
    "topic2": [
        "{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}",
        "{\"anotherfield1\":\"anothervalue1\",\"anotherfield2\":\"anothervalue2\"}"
    ]
}

Slash quote (") symbol is just a properly escaped quotation in JSON. Your parser didn't recognize internal JSONs as JSONs but took them as Strings. Therefore within a String it escaped all " symbols.斜线引号 (") 符号只是 JSON 中正确转义的引号。您的解析器没有将内部 JSON 识别为 JSON,而是将它们视为字符串。因此在字符串中它转义了所有 " 符号。 I suggest that you can use class ObjectMapper of Json-Jackson (also known as Faster XML) library (Maven artifacts here ).我建议你可以使用Json-Jackson(也称为 Faster XML)库的 class ObjectMapper这里是 Maven artifacts)。 I wrote my own open source library called MgntUtils, that has JSON parser based on Json-Jackson.我编写了自己的开源库 MgntUtils,它具有基于 Json-Jackson 的 JSON 解析器。 Using this library you can easily parse your JSON String into a Map使用这个库,您可以轻松地将 JSON 字符串解析为 Map

Map<String, Object> myMap = null;
try {
      myMap = JsonUtils.readObjectFromJsonString(jsonString, Map.class);
    }
} catch (IOException e) {
   ...
}

Here is Javadoc for JsonUtils .这是JsonUtils的 Javadoc。 The Maven artifacts for MgntUtils library could be found here , and library as a jar along with Javadoc and source code could be found on Github here MgntUtils 库的 Maven 工件可以在这里找到,作为 jar 的库以及 Javadoc 和源代码可以在 Github 上找到

It is now working using these changes:它现在正在使用这些更改:

HashMap<String,List<Object>> responses = new HashMap<String,List<Object>>();

for (ConsumerRecord<String,String> consumerRecord : records) {
    if(!responses.containsKey(consumerRecord.topic())){
        responses.put(consumerRecord.topic(), new ArrayList<Object>());
    }
    responses.get(consumerRecord.topic()).add(new JSONObject(consumerRecord.value()));
}

jsonObject = new JSONObject(responses);
return jsonObject.toMap();

  1. Convert Kafka message string to JSONObject new JSONObject(consumerRecord.value())将 Kafka 消息字符串转换为 JSONObject new JSONObject(consumerRecord.value())
  2. Construct a JSONObject from a Map using jsonObject = new JSONObject(responses);使用jsonObject = new JSONObject(responses);
  3. Return Map<String, Object> using jsonObject.toMap();返回 Map<String, Object> 使用jsonObject.toMap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM