Most suitable mapping of JSON to Java object

Asked

Viewed 1,003 times

0

I need to work with a JSON that has recipe information. This JSON has an object (Recipe) with some object arrays (Ingredients, Steps).

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

Screenshot of json (no Steps array appears). inserir a descrição da imagem aqui

  • Do not use screenshots instead of code, please. This disrupts the search and disrupts the reading by screen reader users.

1 answer

1


Use maps when the key is always the same (always id, name, ingredients) doesn’t 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. The problem is that when we work with JSON, these classes explode in quantity. What we do is we use some library that creates this mapping for us and some tool that generates the classes automatically based on JSON.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.