T
T
Therapyx2017-05-29 18:04:59
Java
Therapyx, 2017-05-29 18:04:59

How to parse JSON with different data structure (java)?

Previously, I did not have to work with JSON, but now there is a task:
We need to load a JSON file with the following data for further work. For example, sort them into categories in a dropdownlist.
The data itself is some Points of interests of the following format

[{
 "type": "amenity",
 "name": "Uni",
 "address": "test 11",
 "lat": 49.8670679,
 "lon": 8.6346578,
 "icon": "test.jpg"
 },
 { "type": "fuel",
 "name": "jet",
 "address": "test75",
 "lat": 49.8796429,
 "lon": 8.5743928
 },
 { "type": "pub",
 "name": "La Bodega",
 "address": "test34",
 "capacity": 120,
 "lat": 49.879903,
 "lon": 8.6454108
 }]

As you can see here, there is always Name, adress, lat, lon,
but it can also be icon or capasity or nothing.
Actually, the question itself ... What is the best decision that can be made in this situation? Switchcase and a ton of shit of the year distributing objects depending on the type?
Or make the main POI class with Name, adress, lat, lon, and then add possible differences by inheritance?
Or is there still a more humane way when working with JSON files that I don't know about? ))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
al_gon, 2017-05-29
@Therapyx

Gson, Switchcase and a ton of shit allocating objects by type?

X
x67, 2017-05-29
@x67

And how are you going to distribute data across classes without conditions in one form or another? If the operator ifwere so terrible, then they would have got rid of it long ago, as well as from goto.
You can do it elegantly and universally - do it. But more often there is a choice - either beautiful or comfortable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question