简体   繁体   中英

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.

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. I suggest that you can use class ObjectMapper of Json-Jackson (also known as Faster XML) library (Maven artifacts here ). I wrote my own open source library called MgntUtils, that has JSON parser based on Json-Jackson. Using this library you can easily parse your JSON String into a Map

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

Here is Javadoc for JsonUtils . 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

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())
  2. Construct a JSONObject from a Map using jsonObject = new JSONObject(responses);
  3. Return Map<String, Object> using jsonObject.toMap();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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