2

I have an android app which makes a call do rest api. It returns a list of restaurants. Each restaurant has list of categories. Each category can have multiple dishes.

When I deserialize this list, I have nice ArrayList of Restaurants, each Restaurant also has an ArrayList of Categories, but in Category class, ArrayList of Dishes is null.

Does anyone has any idea how to solve this?

JSON response: (it's actually a object of type ServiceResponse)

{
 "requestToken" : 0,
  "responseCode" : 2,
  "objectType" : "com.test.Restaurant",
  "error" : null,
 "data" : [ {
    "id" : 1,
    "name" : "Name",
    "addressCity" : "Warszawa",
    "addressStreet" : "Kopernika",
    "addressStreetNumber" : "4",
    "addressApartmentNumber" : "17",
    "categories" : [ {
      "id" : 1,
      "restaurantId" : 1,
      "name" : "Breakfast",
      "description" : "Description",
      "dishes" : [ {
        "id" : 11,
        "categoryId" : 1,
        "name" : "Dish 1",
        "description" : "Descr",
        "price" : 8.99,
        "photoId" : 51
      }, {
        "id" : 21,
        "categoryId" : 1,
        "name" : "Dish 2",
        "description" : "Descr",
        "price" : 6.99,
        "photoId" : 41
      }, {
        "id" : 31,
        "categoryId" : 1,
        "name" : "Dish 3",
        "description" : "Descr",
        "price" : 14.99,
        "photoId" : 61
      } ]
    }, {
      "id" : 2,
      "restaurantId" : 1,
      "name" : "Obiady",
      "description" : "Opis kategorii",
      "dishes" : [ {
        "id" : 41,
        "categoryId" : 2,
        "name" : "Danie 4",
        "description" : "Descr",
        "price" : 6.99,
        "photoId" : 0
      }, {
        "id" : 61,
        "categoryId" : 2,
        "name" : "Danie 5",
        "description" : "Descr",
        "price" : 8.0,
        "photoId" : 0
      }, {
        "id" : 101,
        "categoryId" : 2,
        "name" : "Nowy obiad",
        "description" : "",
        "price" : 111.0,
        "photoId" : 0
      } ]
    }, {
      "id" : 3,
      "restaurantId" : 1,
      "name" : "Pasta",
      "description" : "Opis kategorii",
      "dishes" : [ ]
    }, {
      "id" : 4,
      "restaurantId" : 1,
      "name" : "Przystawki",
      "description" : "Opis kategorii",
     "dishes" : [ ]
    }, {
      "id" : 11,
      "restaurantId" : 1,
      "name" : "Some category",
      "description" : "test",
      "dishes" : [ ]
    }, {
      "id" : 51,
      "restaurantId" : 1,
      "name" : "Sushi",
      "description" : "",
      "dishes" : [ ]
    } ]
  }]
  }

Parsing result with Jackson:

ServiceResponse response = getServiceOutput(params, httpMethod);

if(response.getData() != null && response.getData().size() > 0) {
    try {
        // convert data array from object to class defined in objectType (in JSON)
        ObjectMapper mapper = new ObjectMapper();
        Class clazz = Class.forName( response.getObjectType() );
        ArrayList list = (mapper.convertValue(response.getData(), 
            TypeFactory.defaultInstance().constructCollectionType(
                    ArrayList.class, clazz)));

        broadcastIntent.putExtra("result", list);
        broadcastIntent.putExtra("response_code", ServiceResponse.OK);
    } catch (Exception e) {
        broadcastIntent.putExtra("response_code", ServiceResponse.ERROR);
        e.printStackTrace();
    }
}
3
  • could you provide the object hierarchy /the code for the classes you are using (esp. Category.java) Commented Dec 15, 2014 at 10:13
  • Well. I think it's pretty simple. Restaurant has ArrayList<Category> categories; Categories has ArrayList<Dish> dishes; Is that what you asked about? Commented Dec 15, 2014 at 10:16
  • yep, hoped for simple typo in variable,getter/setter or annotation that caused the issue, since every other class got parsed correctly Commented Dec 15, 2014 at 10:20

1 Answer 1

1

This works for me. The reason I'm declaring the classes (Response, Restaurant etc) as static is because Jackson cannot deserialize non-static inner classes.

 public class Test {

static String jsonString = "{ \"requestToken\":0, \"responseCode\":2, \"objectType\":\"com.test.Restaurant\", \"error\":null, \"data\":[ { \"id\":1, \"name\":\"Name\", \"addressCity\":\"Warszawa\", \"addressStreet\":\"Kopernika\", \"addressStreetNumber\":\"4\", \"addressApartmentNumber\":\"17\", \"categories\":[ { \"id\":1, \"restaurantId\":1, \"name\":\"Breakfast\", \"description\":\"Description\", \"dishes\":[ { \"id\":11, \"categoryId\":1, \"name\":\"Dish 1\", \"description\":\"Descr\", \"price\":8.99, \"photoId\":51 }, { \"id\":21, \"categoryId\":1, \"name\":\"Dish 2\", \"description\":\"Descr\", \"price\":6.99, \"photoId\":41 }, { \"id\":31, \"categoryId\":1, \"name\":\"Dish 3\", \"description\":\"Descr\", \"price\":14.99, \"photoId\":61 } ] }, { \"id\":2, \"restaurantId\":1, \"name\":\"Obiady\", \"description\":\"Opis kategorii\", \"dishes\":[ { \"id\":41, \"categoryId\":2, \"name\":\"Danie 4\", \"description\":\"Descr\", \"price\":6.99, \"photoId\":0 }, { \"id\":61, \"categoryId\":2, \"name\":\"Danie 5\", \"description\":\"Descr\", \"price\":8.0, \"photoId\":0 }, { \"id\":101, \"categoryId\":2, \"name\":\"Nowy obiad\", \"description\":\"\", \"price\":111.0, \"photoId\":0 } ] }, { \"id\":3, \"restaurantId\":1, \"name\":\"Pasta\", \"description\":\"Opis kategorii\", \"dishes\":[ ] }, { \"id\":4, \"restaurantId\":1, \"name\":\"Przystawki\", \"description\":\"Opis kategorii\", \"dishes\":[ ] }, { \"id\":11, \"restaurantId\":1, \"name\":\"Some category\", \"description\":\"test\", \"dishes\":[ ] }, { \"id\":51, \"restaurantId\":1, \"name\":\"Sushi\", \"description\":\"\", \"dishes\":[ ] } ] } ] }";

public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Response res = mapper.readValue(jsonString, Response.class);
}

public static class Response {
    public String requestToken;
    public List<Restaurant> data;

    public Response(){}

    public String getRequestToken() {
        return requestToken;
    }

    public void setRequestToken(String requestToken) {
        this.requestToken = requestToken;
    }

    public List<Restaurant> getData() {
        return data;
    }

    public void setData(List<Restaurant> data) {
        this.data = data;
    }
}

public static class Restaurant {
    public List<Category> categories;

    public List<Category> getCat() {
        return categories;
    }

    public void setCat(List<Category> categories) {
        this.categories = categories;
    }

    public Restaurant(){}
}

public static class Category {
    public List<Dish> dishes;

    public Category(){}

    public List<Dish> getDishes() {
        return dishes;
    }

    public void setDishes(List<Dish> dishes) {
        this.dishes = dishes;
    }
}

public static class Dish {

    public String id, categoryId, name, description;
    public float price;
    public int photoId;

    public Dish(){}

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

3 Comments

I've added it, but still no luck.
Please take a loot at my updated answer with functional source code.
Is there a way I could do this with List<Object> in Response class? So that I could put there all objects, not just restaurant?

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.