从json数组中查找错误的Json对象

时间:2022-08-22 14:12:17

I have following type of JSON array (actually I received it as string so I'm trying to convert it to JSON array),

我有以下类型的JSON数组(实际上我收到它作为字符串,所以我试图将其转换为JSON数组),

[{"Message":{"AccountId":"0","CreationDate":"02-DEC-16","Sbu":null,"ProfileId":"28261723","messageSeqId":69},"Offset":6},
{"Message":{"AccountId":"0","CreationDate":"02-DEC-16","Sbu":null,"ProfileId":"28261271","messageSeqId":76},"Offset":7},
{"Message":{"AccountId":"0","CreationDate":"06-DEC-16","Sbu":null,"ProfileId":"28261871","messageSeqId":99},"Offset":8},
{"Message":{"AccountId":"0","CreationDate":"06-DEC-16","Sbu":null,"ProfileId":"28261921","messageSeqId":101},"Offset":9},
{"Message":{"AccountId":"0","CreationDate":"07-DEC-16","Sbu":null,"ProfileId":"28260905","messageSeqId":105},"Offset":10}]

Sometimes this JSON array parsing fails because one JSON objects has fails to parse (I'm using JSON.simple to the JSON parsing). Is there a way to identify the erroneous JSON object?

有时这个JSON数组解析失败,因为一个JSON对象无法解析(我正在使用JSON.simple进行JSON解析)。有没有办法识别错误的JSON对象?

Here is the code part(ResponseJson is above string that want to convert to JSON array),

这是代码部分(ResponseJson位于要转换为JSON数组的字符串之上),

JSONParser jsonParser = new JSONParser();
    try{
        JSONArray jsonArray = (JSONArray) jsonParser.parse(ResponseJson);
        int jsonArrayLength = jsonArray.size();
        System.out.println("jsonArray length: " + jsonArrayLength);
        if (jsonArrayLength > 0) {
            subscribeMessageEvent(topic,qStart,jsonArrayLength,jsonArray);
        }
    }catch (Exception e){
        e.printStackTrace();
    }

1 个解决方案

#1


0  

No, you can't identify which JSON Object is not properly formed with your current implementation.

不,您无法识别当前实现未正确形成哪个JSON对象。

Anyways, if you're receiving your input as a String, you could split it into the different messages and then parse them separately. That way you're in control and you can decide what to do with them individually.

无论如何,如果您以字符串形式接收输入,则可以将其拆分为不同的消息,然后单独解析它们。这样你就可以控制,你可以决定如何单独使用它们。

#1


0  

No, you can't identify which JSON Object is not properly formed with your current implementation.

不,您无法识别当前实现未正确形成哪个JSON对象。

Anyways, if you're receiving your input as a String, you could split it into the different messages and then parse them separately. That way you're in control and you can decide what to do with them individually.

无论如何,如果您以字符串形式接收输入,则可以将其拆分为不同的消息,然后单独解析它们。这样你就可以控制,你可以决定如何单独使用它们。