JSON best mapping for Java object [closed]

0

I need to work with a JSON that has revenue information. This JSON has an object (Recipe) with some object arrays (ingredients, steps).

What would be most appropriate, from the point of view of object orientation: create 3 classes - Recipes , Ingredients , Steps - with attributes received by JSON (id, name for the class Recipes , quantity, measure, ingredient for the class Ingredients, etc ...) or create a single class (Recipes) with the attributes of it (id, name) and two ArrayList of HashMap with the values of Ingredients and Steps? And why?

json Screenshot (Steps array does not appear).

    
asked by anonymous 08.05.2017 / 06:47

1 answer

1
Using maps when the key is always the same (always id , name , ingredients ) does not make much sense. The ideal, from the point of view of leaving your code more expressive, is to have a class for each concept that repeats itself. The problem is that when we work with JSON, these classes explode in quantity. What we do is use some library that creates this mapping for us and some tool that generates classes automatically based on JSON.

Try the Jackson or the GSON for mapping and jsonschema2pojo.org to generate the classes for you.

    
08.05.2017 / 08:39