处理字段内的嵌套 JSON 会导致 Java 中的未终止对象错误

问题描述 投票:0回答:1

我有一个像这样的 JSON 字符串:

{"log":"2024-07-04T12:00:57.345665213+02:00 stdout F 2024-07-04 12:00:57,345 TRACE [http-nio-8080-exec-6] org.zalando.logbook.DefaultHttpLogWriter: {"origin":"local","type":"response","correlation":"8c90aa3c5b5bb919","duration":0,"protocol":"HTTP/1.1","status":200,"headers":{"Connection":["close"],"Content-Type":["application/vnd.spring-boot.actuator.v3+json"],"Date":["Thu, 04 Jul 2024 10:00:57 GMT"],"Transfer-Encoding":["chunked"]},"body":{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":151490334720,"free":96213721088,"threshold":10485760,"exists":true}},"livenessState":{"status":"UP"},"ping":{"status":"UP"},"readinessState":{"status":"UP"}},"groups":["liveness","readiness"]}}"}

当我尝试使用以下方法解析它时:

JsonObject messageObject = JsonParser.parseString(jsonString).getAsJsonObject();

由于“日志”字段内嵌套 JSON,我遇到了未终止对象错误。如何处理 JSON 字符串在字段内包含嵌套 JSON 的情况而不导致解析错误?具体来说,我需要提取有效的 JSON 部分(例如“log”中的内部对象),即使它们嵌套在另一个字段中。

java json
1个回答
0
投票

JSON 文档格式不正确,因此解析器无法解析它。

我认为有些字符应该转义。

© www.soinside.com 2019 - 2024. All rights reserved.