如何使用 json 对象数组将 HashMap<String,List<Object>> 正确转换为 Json
How to properly convert HashMap<String,List<Object>> to Json with array of json object
我有一个 Kafka 消息流,想构建一个 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()));
}
预期响应:
{
"topic1": [
{"field1":"value1","field2":"value2","field3":"value3"}
],
"topic2": [
{"field1":"value1","field2":"value2","field3":"value3"},
{"anotherfield1":"anothervalue1","anotherfield2":"anothervalue2"}
]
}
实际回复:
{
"topic1": [
"{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}"
],
"topic2": [
"{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}",
"{\"anotherfield1\":\"anothervalue1\",\"anotherfield2\":\"anothervalue2\"}"
]
}
斜线引号 (") 符号只是 JSON 中的一个正确转义引号。您的解析器没有将内部 JSON 识别为 JSON,而是将它们视为字符串。因此在 String 中它转义了所有 " 符号。我建议你可以使用 class ObjectMapper of Json-Jackson (also known as Faster XML) library (Maven artifacts here)。我编写了自己的名为 MgntUtils 的开源库,它具有基于 Json-Jackson 的 JSON 解析器。使用此库,您可以轻松地将 JSON 字符串解析为 Map
Map<String, Object> myMap = null;
try {
myMap = JsonUtils.readObjectFromJsonString(jsonString, Map.class);
}
} catch (IOException e) {
...
}
这是 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
的 Javadoc
它现在正在使用这些更改:
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();
- 将 Kafka 消息字符串转换为 JSONObject
new JSONObject(consumerRecord.value())
- 使用 Map 从 Map 构造 JSONObject
jsonObject = new JSONObject(responses);
- Return Map
使用 jsonObject.toMap();
我有一个 Kafka 消息流,想构建一个 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()));
}
预期响应:
{
"topic1": [
{"field1":"value1","field2":"value2","field3":"value3"}
],
"topic2": [
{"field1":"value1","field2":"value2","field3":"value3"},
{"anotherfield1":"anothervalue1","anotherfield2":"anothervalue2"}
]
}
实际回复:
{
"topic1": [
"{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}"
],
"topic2": [
"{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}",
"{\"anotherfield1\":\"anothervalue1\",\"anotherfield2\":\"anothervalue2\"}"
]
}
斜线引号 (") 符号只是 JSON 中的一个正确转义引号。您的解析器没有将内部 JSON 识别为 JSON,而是将它们视为字符串。因此在 String 中它转义了所有 " 符号。我建议你可以使用 class ObjectMapper of Json-Jackson (also known as Faster XML) library (Maven artifacts here)。我编写了自己的名为 MgntUtils 的开源库,它具有基于 Json-Jackson 的 JSON 解析器。使用此库,您可以轻松地将 JSON 字符串解析为 Map
Map<String, Object> myMap = null;
try {
myMap = JsonUtils.readObjectFromJsonString(jsonString, Map.class);
}
} catch (IOException e) {
...
}
这是 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
的 Javadoc它现在正在使用这些更改:
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();
- 将 Kafka 消息字符串转换为 JSONObject
new JSONObject(consumerRecord.value())
- 使用 Map 从 Map 构造 JSONObject
jsonObject = new JSONObject(responses);
- Return Map
使用 jsonObject.toMap();