【问题标题】:Jackson Custom deserializer ArrayIndexOutOfBoundsExceptionJackson 自定义反序列化器 ArrayIndexOutOfBoundsException
【发布时间】:2016-02-13 22:15:21
【问题描述】:

我正在尝试使用一个 Web 服务(我没有电源),它为我提供 JSON 中的对象数组。结果的格式有点不正确:

[
  [ #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)

这让我觉得杰克逊没有使用我的自定义解串器,或者我错过了一些东西。

【问题讨论】:

    标签: java json jersey jackson


    【解决方案1】:

    好的,我会给你答案,因为你编写了大部分代码,解决方案是:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个代码:

      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;
      }
      

      【讨论】:

      • 好的,所以我对它进行了一些修改以供 java 编译: public Pojo[] deserialize [...] { ObjectCodec oc = jParser.getCodec(); JsonNode 节点 = oc.readTree(jParser); [...] next.get("attribute1").asText(), next.get("attribute2").asText())); } return (Pojo[]) pojos.toArray();但是我在 next.get("attribute2") 上得到了一个 nullpointerexception 甚至很难我可以看到它在手动查询时实际上有一个值。
      猜你喜欢
      • 2011-04-12
      • 2016-01-29
      • 1970-01-01
      • 2019-08-10
      • 2016-02-12
      • 1970-01-01
      • 2016-01-11
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多