开发者

Not able to deserialize an object with with List using Gson api

Getting the following exception while deserializing an object:

com.google.gson.JsonParseException: 
  The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@1e2befa 
    failed to deserialized json object 
      {"com.gsicommerce.analytics.platform.model.webstore.AnalyticsProduct":
          [
              {"id":"3680231","longTitle":"Graco SnugRide Infant Car Seat - Pippin","available":"true"}
          ]
      } 
    given the type com.google.gson.ParameterizedTypeImpl@b565dd

Here is the class that I am trying to deserialize开发者_高级运维:

public class TrusRegWishAddItemEvent implements Serializable {
    static final long serialVersionUID = 1L;
    private final List<AnalyticsProduct> items;

    private TrusRegWishAddItemEvent() {
        items = null;
    }

    public TrusRegWishAddItemEvent(List<AnalyticsProduct> items) {
        this.items = items;
    }

    public List<AnalyticsProduct> getItems() {
        return items;
    }      
}

public class AnalyticsProduct implements Serializable {
    static final long serialVersionUID = 1L;
    private final long id;
    private final String longTitle;
    private final boolean available;

    public AnalyticsProduct() {
        id = 0;
        longTitle = null;
        available = false;
    }

    public AnalyticsProduct(long id, String longTitle, boolean available) {
        this.id = id;
        this.longTitle = longTitle;
        this.available = available;
    }

    public long getId() {
        return id;
    }

    public String getLongTitle() {
        return longTitle;
    }

    public boolean isAvailable() {
        return available;
    }
}

Please guide.


If the JSON is

{
  "items":
  [
    {
      "id":"3680231",
      "longTitle":"Graco SnugRide Infant Car Seat - Pippin",
      "available":"true"
    }
  ]
}

then the following example uses Gson to easily deserialize/serialize to/from the same Java data structure in the original question.

  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    TrusRegWishAddItemEvent thing = gson.fromJson(new FileReader("input.json"), TrusRegWishAddItemEvent.class);
    System.out.println(gson.toJson(thing));
  }

If instead the JSON must be

  {"com.gsicommerce.analytics.platform.model.webstore.AnalyticsProduct":
      [
          {"id":"3680231","longTitle":"Graco SnugRide Infant Car Seat - Pippin","available":"true"}
      ]
  }

then it's necessary to translate the JSON element name "com.gsicommerce.analytics.platform.model.webstore.AnalyticsProduct" to the Java member items. To do so, Gson provides a couple of mechanisms, the simplest of which is to just annotate the items attribute in TrusRegWishAddItemEvent as follows.

class TrusRegWishAddItemEvent implements Serializable
{
  static final long serialVersionUID = 1L;

  @SerializedName("com.gsicommerce.analytics.platform.model.webstore.AnalyticsProduct")
  private final List<AnalyticsProduct> items;

  ...
}

But without this @SerializedName annotation Gson doesn't throw an exception when attempting to deserialize, instead it just constructs a TrusRegWishAddItemEvent instance with items as a null reference. So, it's not clear what was done to generate the error message in the original question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜