[英]Jackson Custom deserializer ArrayIndexOutOfBoundsException
我正在尝试使用为我提供 JSON 对象数组的 Web 服务(我对此无能为力)。 结果是一种格式错误的形式:
[
[ #this is first object
{
"attribute1":"value1"
},
{
"attribute2":"value2"
}
],
[ # this is second object
{
"attribute1":"value1"
},
{
"attribute2":"value2"
}
]
]
因此,我尝试使用 jersey 客户端 2.22.1 和 jackson core 2.5.4 将其反序列化为 pojo。 由于基本的 Jackson 反序列化不起作用,我创建了一个自定义反序列化器。
Pojo类:
@JsonDeserialize(using = MyDeserializer.class)
public class Pojo {
private String attribute1;
private String attribute2;
*default constructor, getter and setters*
}
MyDeserializer 类:
public class MyDeserializer extends JsonDeserializer<Pojo> {
@Override
public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Pojo pojoObj = new Pojo();
while (jParser.nextToken() != JsonToken.END_ARRAY) {
String fieldname = jParser.getCurrentName();
if ("attribute1".equals(fieldname)) {
jParser.nextToken();
pojoObj.setAttribute1(jParser.getText());
}
if ("attribute2".equals(fieldname)) {
jParser.nextToken();
pojoObj.setAttribute2(jParser.getText());
}
}
jParser.close();
return pojoObj;
}
}
球衣/杰克逊电话:
Client client = ClientBuilder.newClient().register(JacksonJaxbJsonProvider.class);
WebTarget webTarget = client.target("http://server/service/ressource").queryParam("param1", value);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
Response response = invocationBuilder.get();
list = Arrays.asList(response.readEntity(Pojo[].class));
但现在当我调用它时,我得到:
java.lang.ArrayIndexOutOfBoundsException: 1054
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2732)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:652)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:149)
这让我觉得要么杰克逊没有使用我的自定义解串器,要么我错过了一些东西。
试试这个代码:
public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
Iterator<JsonNode> iterator = node.iterator();
List<Pojo> pojos = new ArrayList<Pojo>();
while (iterator.hasNext()) {
JsonNode next = iterator.next();
pojos.add(
new Pojo(
next.get("attribute1"),
next.get("attribute2")));
}
return pojos;
}
好的,因为您编写了大部分代码,所以我会给您答案,解决方案是:
public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
Iterator<JsonNode> iterator = node.iterator();
JsonNode next = iterator.next();
String attribute1 = null
if (next.get("attribute1") != null) {
attribute1 = next.get("attribute1").asText();
}
next = iterator.next();
String attribute2 = null
if (next.get("attribute2") != null) {
attribute2 = next.get("attribute2").asText();
}
Pojo objPojo = new Pojo(attribute1,attribute2);
return objPojo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.