Question

We are consuming a REST webservice (Shopware) to fetch product information. Our problem are the categories which are always null.

The json which comes over the wire looks like this:

{
   "data":{
      "id":3,
      "mainDetail":{
         "id":3,
         "articleId":3,
         "categories":{
            "14":{
               "id":14,
               "name":"Edelbr\u00e4nde"
            },
            "21":{
               "id":21,
               "name":"Produktvergleiche & Filter"
            },
            "50":{
               "id":50,
               "name":"Brandies"
            }
         },
         "success":true
      }
   }
}

Our Articles class is defined like this:

@XmlElement(name = "categories")
public Category[] getCategories() {
       return categories;
}

public void setCategories(Category[] categories) {

       this.categories = categories;
}



    Articles articles = apiClient.getClient().resource(apiClient.getBaseUrl() + this.getResourcePath()).queryParams(this.params).type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(Articles.class);

// this always returns null
Categories categories = articles.getCategories();  

We are able to access all fields without problems except the categories. We assume it is because the ids which are the keys so that Jersey does not now how to map them to the correct class.

Questions:

  1. Is there a name for this kind of json notation of the categories so that I have something to search in google?

  2. Do you have any hints on jersey annotations, custom type-adapters or something like that which tells the de-serializer to recognize the categories and populate our categories collection?

Was it helpful?

Solution

Reposting the comment of Brian Roach as answer:

since 'categories' in your JSON isn't an array (its an object), its unlikely its going to map to an array in java. Change it to:

Map<String,Category>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top