将JSON有效内容解析为POJO时是否可以省略损坏的对象?
有效负载示例:
[
{
"someField": "this_object_seems_to_be_ok"
},
{
"someBrokenStructure"= "equals_sign_instead_of_colon_here"
}
]
我想只列出一个结构正确的对象。使用任何现有框架都可以实现吗?
答案 0 :(得分:1)
在所有情况下,要为具有预期结果的无效JSON
有效载荷编写反序列化器确实很困难。具有许多嵌套的JSON
和JSON Objects
的{{1}}有效负载可以通过多种方式进行破坏。就您而言,它的结构非常简单,应该易于维护。
因此,您需要实现自定义JSON Arrays
解串器,如下所示:
List
以上代码会显示第一个错误:
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./src/main/resources/test.json");
SimpleModule skipInvalidObjectsModule = new SimpleModule();
skipInvalidObjectsModule.addDeserializer(List.class, new ListJsonDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(skipInvalidObjectsModule);
List<Map<String, Object>> result = mapper.readValue(jsonFile, List.class);
System.out.println(result);
}
}
class ListJsonDeserializer extends JsonDeserializer<List<Object>> {
@Override
public List<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
MapType mapType = ctxt.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
JsonDeserializer<Object> mapDeserializer = ctxt.findRootValueDeserializer(mapType);
List<Object> result = new ArrayList<>();
p.nextToken();
while (p.currentToken() != JsonToken.END_ARRAY) {
if (p.currentToken() == JsonToken.START_OBJECT) {
try {
result.add(mapDeserializer.deserialize(p, ctxt));
} catch (JsonParseException e) {
System.err.println(e.getMessage());
}
}
try {
p.nextToken();
} catch (JsonParseException e) {
System.err.println(e.getMessage());
}
}
return result;
}
}
最终结果:
Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value
at [Source: (File); line: 6, column: 27]
Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 29]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 30]
Unexpected character ('q' (code 113)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 31]
Unexpected character ('u' (code 117)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 32]
Unexpected character ('a' (code 97)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 33]
Unexpected character ('l' (code 108)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 34]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 35]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 36]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 37]
Unexpected character ('i' (code 105)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 38]
Unexpected character ('g' (code 103)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 39]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 40]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 41]
Unexpected character ('i' (code 105)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 42]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 43]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 44]
Unexpected character ('t' (code 116)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 45]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 46]
Unexpected character ('a' (code 97)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 47]
Unexpected character ('d' (code 100)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 48]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 49]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 50]
Unexpected character ('f' (code 102)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 51]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 52]
Unexpected character ('c' (code 99)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 53]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 54]
Unexpected character ('l' (code 108)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 55]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 56]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 57]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 58]
Unexpected character ('h' (code 104)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 59]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 60]
Unexpected character ('r' (code 114)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 61]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 62]
Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 63]
答案 1 :(得分:0)
尽管Michal Ziober的回答在许多情况下会有所帮助,但我发现我可以尝试使用Google的Gson。它具有尝试解析非标准结构的宽松机制。 有关更多信息,请参见this link