[英]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.