James Fox
James Fox

Reputation: 691

JSON Deserialization error in Java

I am trying to deserialize some JSON which I got from the eBay API but I am getting the error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

The JSON that is returned has many levels to it. e.g.

{"searchResult":[{"@count":"100","item":[{"itemId":["281492499859"],"title":["Panasonic Lumix TZ20 Digital Camera in Good Condition"],... etc

This is for a property called itemId in CompletedListing (class defined below)

I am calling this method with the line:

CompletedListingContainer listing = mapper.readValue(new File("C:\\resource\\jsonresult.txt"), CompletedListingContainer.class);`

Here are my classes.

CompletedListingContainer

public class CompletedListingContainer {

  private ArrayList<CompletedListing> item;

  private ArrayList<SearchResult> searchResult;

  private List<String> paginationOutput;

  @JsonIgnore
  public List<String> getPaginationOutput() {
      return paginationOutput;
  }

  public ArrayList<SearchResult> getSearchResult() {
      return searchResult;
  }

  public ArrayList<CompletedListing> getItem() {
      return item;
  } 
}

SearchResult

public class SearchResult {

  @JsonProperty("@count")
  private String count;

  private ArrayList<CompletedListing> item;

  @JsonIgnore
  public String getCount() {
      return count;
  }

  public ArrayList<CompletedListing> getItem() {
      return item;
  }
}

CompletedListing

public class CompletedListing {

  @JsonProperty("itemId")
  private String itemId;

  private String title;

  @JsonProperty("itemId")
  public String getItemId() {
    return itemId;
  }

  @JsonProperty("title")
  public String getTitle() {
    return title;
  }
}

Any help would be greatly appreciated. Let me know if you need any more information.

Upvotes: 0

Views: 598

Answers (2)

Vlad
Vlad

Reputation: 10780

{"itemId":["281492499859"] means your CompletedListing.itemId property should be a list or array.

Upvotes: 1

devops
devops

Reputation: 9179

put private ArrayList<CompletedListing> item in your SearchResult class and remove it from CompletedListingContainer

look at your json file structure:

{
   "searchResult":[
       {
        "count":"100",
        "item":[
               {"itemId":["281492499859"]...
       },
   ....
}

try this online tool to generate Java classes from json

Upvotes: 0

Related Questions