如何将多个josn合并的信息拆分成独立的josn 例如{"Sender":1,"Type":1,"DataJson":"{\"Key\":2116235005,\"ArithmeticResult\":-3,\"IsJudgeAgain\":false,\"CheckUsedSeconds\":0.0,\"ImageData\":null,\"Area\":3,\"WarnNum\":0,\"CheckTime\":\"2023-06-09T09:45:46.7957432+08:00\",\"FlawList\":[],\"IsHalconOK\":0,\"IsColorAIOK\":0,\"IsWhileOK\":0,\"AppType\":1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00031_0.jpg\",\"ID\":\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\",\"CameraSN\":\"CB02678AAK00031\",\"Index\":0,\"SrcImage\":null,\"Pid\":null,\"IsEnd\":false}"}{"Sender":1,"Type":1,"DataJson":"{\"Key\":1548132793,\"ArithmeticResult\":-1,\"IsJudgeAgain\":false,\"CheckUsedSeconds\":0.09,\"ImageData\":null,\"Area\":0,\"WarnNum\":1,\"CheckTime\":\"2023-06-09T09:45:46.7538551+08:00\",\"FlawList\":[{\"ParentID\":1548132793,\"CheckTime\":\"2023-06-09T09:45:46.9583098+08:00\",\"ImageNo\":\"\",\"X\":10.0,\"Y\":10.0,\"Width\":10.0,\"Height\":10.0,\"Configidence\":0.0,\"MeasureValue\":0.0,\"ElementType\":\"亮点3\",\"ArithmeticResult\":-1,\"JudgeAgain\":2,\"Result\":-1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\",\"Key\":-1725364068,\"Order\":0,\"IsSelected\":false,\"ChangedEnabled\":false,\"Errors\":[],\"Error\":\"\",\"IsInDesignMode\":false}],\"IsHalconOK\":0,\"IsColorAIOK\":0,\"IsWhileOK\":0,\"AppType\":1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\",\"ID\":\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\",\"CameraSN\":\"CB02678AAK00009\",\"Index\":0,\"SrcImage\":null,\"Pid\":null,\"IsEnd\":false}"}
时间: 2023-07-11 07:58:33 浏览: 140
这段多个json合并的信息并不是合法的json格式,因此需要先将其转化为合法的json格式,例如将其放入一个数组中,每个json之间用逗号隔开,再用中括号包裹起来,形成一个json数组。假设已经将其转化为如下格式:
```json
[
{"Sender":1,"Type":1,"DataJson":"{\"Key\":2116235005,\"ArithmeticResult\":-3,\"IsJudgeAgain\":false,\"CheckUsedSeconds\":0.0,\"ImageData\":null,\"Area\":3,\"WarnNum\":0,\"CheckTime\":\"2023-06-09T09:45:46.7957432+08:00\",\"FlawList\":[],\"IsHalconOK\":0,\"IsColorAIOK\":0,\"IsWhileOK\":0,\"AppType\":1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00031_0.jpg\",\"ID\":\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\",\"CameraSN\":\"CB02678AAK00031\",\"Index\":0,\"SrcImage\":null,\"Pid\":null,\"IsEnd\":false}"},
{"Sender":1,"Type":1,"DataJson":"{\"Key\":1548132793,\"ArithmeticResult\":-1,\"IsJudgeAgain\":false,\"CheckUsedSeconds\":0.09,\"ImageData\":null,\"Area\":0,\"WarnNum\":1,\"CheckTime\":\"2023-06-09T09:45:46.7538551+08:00\",\"FlawList\":[{\"ParentID\":1548132793,\"CheckTime\":\"2023-06-09T09:45:46.9583098+08:00\",\"ImageNo\":\"\",\"X\":10.0,\"Y\":10.0,\"Width\":10.0,\"Height\":10.0,\"Configidence\":0.0,\"MeasureValue\":0.0,\"ElementType\":\"亮点3\",\"ArithmeticResult\":-1,\"JudgeAgain\":2,\"Result\":-1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\",\"Key\":-1725364068,\"Order\":0,\"IsSelected\":false,\"ChangedEnabled\":false,\"Errors\":[],\"Error\":\"\",\"IsInDesignMode\":false}],\"IsHalconOK\":0,\"IsColorAIOK\":0,\"IsWhileOK\":0,\"AppType\":1,\"ImagePath\":\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\",\"ID\":\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\",\"CameraSN\":\"CB02678AAK00009\",\"Index\":0,\"SrcImage\":null,\"Pid\":null,\"IsEnd\":false}"}
]
```
可以使用Json库(例如Jackson)将这个json数组解析成一个Java数组,然后遍历Java数组,获取每个独立的json字符串,再将其解析成JsonObject即可。以下是示例代码:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
String input = "[{\"Sender\":1,\"Type\":1,\"DataJson\":\"{\\\"Key\\\":2116235005,\\\"ArithmeticResult\\\":-3,\\\"IsJudgeAgain\\\":false,\\\"CheckUsedSeconds\\\":0.0,\\\"ImageData\\\":null,\\\"Area\\\":3,\\\"WarnNum\\\":0,\\\"CheckTime\\\":\\\"2023-06-09T09:45:46.7957432+08:00\\\",\\\"FlawList\\\":[],\\\"IsHalconOK\\\":0,\\\"IsColorAIOK\\\":0,\\\"IsWhileOK\\\":0,\\\"AppType\\\":1,\\\"ImagePath\\\":\\\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00031_0.jpg\\\",\\\"ID\\\":\\\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\",\\\"CameraSN\\\":\\\"CB02678AAK00031\\\",\\\"Index\\\":0,\\\"SrcImage\\\":null,\\\"Pid\\\":null,\\\"IsEnd\\\":false}\"},{\"Sender\":1,\"Type\":1,\"DataJson\":\"{\\\"Key\\\":1548132793,\\\"ArithmeticResult\\\":-1,\\\"IsJudgeAgain\\\":false,\\\"CheckUsedSeconds\\\":0.09,\\\"ImageData\\\":null,\\\"Area\\\":0,\\\"WarnNum\\\":1,\\\"CheckTime\\\":\\\"2023-06-09T09:45:46.7538551+08:00\\\",\\\"FlawList\\\":[{\\\"ParentID\\\":1548132793,\\\"CheckTime\\\":\\\"2023-06-09T09:45:46.9583098+08:00\\\",\\\"ImageNo\\\":\\\"\\\",\\\"X\\\":10.0,\\\"Y\\\":10.0,\\\"Width\\\":10.0,\\\"Height\\\":10.0,\\\"Configidence\\\":0.0,\\\"MeasureValue\\\":0.0,\\\"ElementType\\\":\\\"亮点3\\\",\\\"ArithmeticResult\\\":-1,\\\"JudgeAgain\\\":2,\\\"Result\\\":-1,\\\"ImagePath\\\":\\\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\\\",\\\"Key\\\":-1725364068,\\\"Order\\\":0,\\\"IsSelected\\\":false,\\\"ChangedEnabled\\\":false,\\\"Errors\\\":[],\\\"Error\\\":\\\"\\\",\\\"IsInDesignMode\\\":false}],\\\"IsHalconOK\\\":0,\\\"IsColorAIOK\\\":0,\\\"IsWhileOK\\\":0,\\\"AppType\\\":1,\\\"ImagePath\\\":\\\"\\\\\\\\SZL-20230222ORR\\\\HMJImages\\\\2023\\\\06\\\\09\\\\09\\\\3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\\SrcImages\\\\230609094546525_CB02678AAK00009_0.jpg\\\",\\\"ID\\\":\\\"3TE55G23064401AC9510513;_Test_Test_Test-202306090947178\\\",\\\"CameraSN\\\":\\\"CB02678AAK00009\\\",\\\"Index\\\":0,\\\"SrcImage\\\":null,\\\"Pid\\\":null,\\\"IsEnd\\\":false}\"}]";
ObjectMapper mapper = new ObjectMapper();
JSONObject[] jsonObjects = mapper.readValue(input, JSONObject[].class);
for (JSONObject jsonObject : jsonObjects) {
System.out.println(jsonObject.toString());
}
```
上述代码中,使用Jackson将json数组解析成一个JSONObject数组,然后遍历这个数组,获取每个独立的json字符串,再将其解析成JsonObject,最终可以对每个独立的JsonObject进行操作。
阅读全文