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
Post a Comment