【发布时间】:2020-06-10 12:19:12
【问题描述】:
我有一个带有键值对的 JSON 文件,我想将键值对放入标题中。 所以当我有一个内容如下的文件时:
[{"msgId": "8600C5A3-C666-4E63-BFDB-52BCF557F938", "jiraId": "ERR002"}]
我想创建名称为 msgId 且值为“8600C5A3-C666-4E63-BFDB-52BCF557F938”等的标头。
或者作为替代方案:有没有办法将交换的标头存储到文件中,以后可以在另一个交换中恢复标头?
谢谢。
编辑:我的例子。
public void jsonToHeaders(String body, @Headers Map<String, String> headers) throws ParseException {
LOG.info("Starting JSON conversion...");
LOG.debug("Body input, content: {} ", body);
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(body);
if (jsonObject != null)
{
String stringValue = null;
String stringKey = null ;
final String NA_STRING = "*** N/A ***";
for (Object key : jsonObject.keySet()) {
stringKey = ((key == null) ? NA_STRING : (String)key);
stringValue = ((jsonObject.get(stringKey) == null) ? NA_STRING : jsonObject.get(stringKey).toString());
headers.put(stringKey, stringValue);
LOG.debug("Processing key {} with value {}", stringKey, stringValue);
}
LOG.info("Done processed JSON: {}", headers.toString());
}
}
【问题讨论】:
标签: json spring-boot apache-camel spring-camel