java - What is an elegant way to obtain (variably available) JSON data? -


i looking elegant way extract data json file, should data consistent simple code, want take account possibility not.

for example

json example 1:

... "years": [{     "id": 100531911,     "year": 2012,     "styles": [{         "id": 101395591,         "name": "lt 2dr convertible w/2lt (3.6l 6cyl 6m)",         "submodel": {             "body": "convertible",             "modelname": "camaro convertible",             "nicename": "convertible"         },         "trim": "lt"     }] }], ... 

json example 2:

... "years": [{     "year": 2012,     "styles": [{         "id": 101395591,         "name": "lt 2dr convertible w/2lt (3.6l 6cyl 6m)",         "submodel": {             "body": "convertible",         },         "trim": "lt"     }] }], ... 

as can see second json example missing data, prevent did following, little messy:

for example if want "nicename" element. in order prevent nullpointerexceptions have run series of if statements:

if (jsonobj.has("years")) {      if (jsonobj.getjsonobject("years").has("submodel")) {          if(jsonobj.getjsonobject("years").getjsonoject("submodel").has("nicename")) {         //do stuff         }     } } 

is there cleaner way approach this?


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -