0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this was the problem. I didn't realise it needed to be a List.
@JamesFox glad to help, please note that same goes for title
Yes, I am changing for all the properties as I have more that I didn't add to the question.
0

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

1 Comment

This hasn't fixed the issue but I can see that it was wrong to have that in the CompletedListingContainer class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.