So i have this data as below and i am trying to parse the data, i can see a lot about parsing JSONArrays online however, not so much on objects within objects. I have had a go and am getting an error. Here is the Json response I am only trying to return the marked fields:
{
"geomagnetic-field-model-result": {
"model": "wmm",
"model_revision": "2020",
"date": {
"value": "2020-07-14"
},
"coordinates": {
"latitude": {
"units": "deg (north)",
"value": 0.0
},
"longitude": {
"units": "deg (east)",
"value": 0.0
},
"altitude": {
"units": "km",
"value": 0.00
}
},
"field-value": {
"total-intensity": {
"units": "nT",
"value": 123 //Return this*****************
},
"declination": {
"units": "deg (east)",
"value": -123 //Return this*****************
},
"inclination": {
"units": "deg (down)",
"value": 123 //Return this*****************
},
"north-intensity": {
"units": "nT",
"value": 123
},
"east-intensity": {
"units": "nT",
"value": -123
},
"vertical-intensity": {
"units": "nT",
"value": 123
},
"horizontal-intensity": {
"units": "nT",
"value": 123
}
},
"secular-variation": {
"total-intensity": {
"units": "nT/y",
"value": 123
},
"declination": {
"units": "arcmin/y (east)",
"value": 123
},
"inclination": {
"units": "arcmin/y (down)",
"value": 123
},
"north-intensity": {
"units": "nT/y",
"value": 123
},
"east-intensity": {
"units": "nT/y",
"value": 123
},
"vertical-intensity": {
"units": "nT/y",
"value": 123
},
"horizontal-intensity": {
"units": "nT/y",
"value": 123
}
}
}
}
My attempt at parsing currently looks like this:
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = response;
String totalIntensity = jsonObject.getJSONObject("field-value").getJSONObject("total-intensity").getString("value");
String declination = jsonObject.getJSONObject("field-value").getJSONObject("declination").getString("value");
String inclination = jsonObject.getJSONObject("field-value").getJSONObject("inclination").getString("value");
} catch (JSONException e) {
Log.d("geoData", "Error recorded");
e.printStackTrace();
}
}
Am i totally wrong? Hopefully this is very easy for someone to put me right.