gpt4 book ai didi

java - JsonPath SUPPRESS_EXCEPTIONS 似乎不起作用

转载 作者:行者123 更新时间:2023-12-01 18:34:05 25 4
gpt4 key购买 nike

正如标题所说,我试图让 jsonPath 不抛出异常。我也只想读一次 json,然后把东西拿出来。下面是我的代码。我做错了什么?

List<Map<String, Object>> messageList = new ArrayList<>();
for (String message : messages) {
Map<String, Object> theData = new LinkedHashMap<>();

Object document = Configuration
.defaultConfiguration()
.addOptions(Option.SUPPRESS_EXCEPTIONS)
.jsonProvider().parse(message);

theData.put("Pk", JsonPath.read(document, "$.header.equipAssetGuid"));
theData.put("assetLocalDate", JsonPath.read(document, "$.header.assetLocalDate"));

theData.put("msgTs", JsonPath.read(document, "$.header.messageTiestamp"));
theData.put("utcOffset", JsonPath.read(document, "$.header.utcOffset"));
theData.put("msgId", JsonPath.read(document, "$.header.messageId"));
theData.put("lastUpdtdTs", JsonPath.read(document, "$.header.messageTimestamp"));
Object m = JsonPath.read(document, "$.fuelLevels[0].level");
if (m != null) {
theData.put("measurement", m);
}

messageList.add(theData);

}

如果我对路径大惊小怪,它总是会抛出异常。

最佳答案

想出了这个:

List<Map<String, Object>> messageList = new ArrayList<>();
for (String message : messages) {
Map<String, Object> theData = new LinkedHashMap<>();
Configuration conf = Configuration.defaultConfiguration()
.setOptions(Option.SUPPRESS_EXCEPTIONS);
DocumentContext documentContext = JsonPath.using(conf).parse(message);

theData.put("Pk", documentContext.read("$.header.equipAssetGuid"));
theData.put("assetLocalDate", documentContext.read("$.header.assetLocalDate"));

theData.put("msgTs", documentContext.read("$.header.messageTimestamp"));
theData.put("utcOffset", documentContext.read("$.header.utcOffset"));
theData.put("msgId", documentContext.read("$.header.messageId"));
theData.put("lastUpdtdTs", documentContext.read("$.header.messageTimestamp"));
Object m = documentContext.read("$.fuelLevels[0].level");
if (m != null) {
theData.put("measurement", m);
}
messageList.add(theData);
}

关于java - JsonPath SUPPRESS_EXCEPTIONS 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60097963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com