1

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.

3 Answers 3

1

I assume that the response you get in the method is the whole JSON object that you have posted here.

You have a few issues:

  1. Your data is not String it is Double or Integer, so you should use getDouble for example.
  2. You forgot about one additional node geomagnetic-field-model-result.
  3. You don't need to assign internal JSONObject jsonObject you can just use response.

To get to the values you should:

public void onResponse(JSONObject response) {
    try {
        // JSONObject jsonObject = response; you don't need this
        Double totalIntensity = response
            .getJSONObject("geomagnetic-field-model-result")
            .getJSONObject("field-value")
            .getJSONObject("total-intensity")
            .getDouble("value");
        Double declination = response
            .getJSONObject("geomagnetic-field-model-result")
            .getJSONObject("field-value")
            .getJSONObject("declination")
            .getDouble("value");
        Double inclination = response
            .getJSONObject("geomagnetic-field-model-result")
            .getJSONObject("field-value")
            .getJSONObject("inclination")
            .getDouble("value");

        // use it somehow
    } catch (JSONException e) {
        Log.d("geoData", "Error recorded");
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create a Schema and use the return values, it's much easier to work with Example schema for your returned response made from http://www.jsonschema2pojo.org/

    -----------------------------------com.example.Altitude.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Altitude {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Double value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Double getValue() {
return value;
}

public void setValue(Double value) {
this.value = value;
}

}
-----------------------------------com.example.Coordinates.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Coordinates {

@SerializedName("latitude")
@Expose
private Latitude latitude;
@SerializedName("longitude")
@Expose
private Longitude longitude;
@SerializedName("altitude")
@Expose
private Altitude altitude;

public Latitude getLatitude() {
return latitude;
}

public void setLatitude(Latitude latitude) {
this.latitude = latitude;
}

public Longitude getLongitude() {
return longitude;
}

public void setLongitude(Longitude longitude) {
this.longitude = longitude;
}

public Altitude getAltitude() {
return altitude;
}

public void setAltitude(Altitude altitude) {
this.altitude = altitude;
}

}
-----------------------------------com.example.Date.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Date {

@SerializedName("value")
@Expose
private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.Declination.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Declination {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.Declination_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Declination_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.EastIntensity.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EastIntensity {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.EastIntensity_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EastIntensity_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("geomagnetic-field-model-result")
@Expose
private GeomagneticFieldModelResult geomagneticFieldModelResult;

public GeomagneticFieldModelResult getGeomagneticFieldModelResult() {
return geomagneticFieldModelResult;
}

public void setGeomagneticFieldModelResult(GeomagneticFieldModelResult geomagneticFieldModelResult) {
this.geomagneticFieldModelResult = geomagneticFieldModelResult;
}

}
-----------------------------------com.example.FieldValue.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class FieldValue {

@SerializedName("total-intensity")
@Expose
private TotalIntensity totalIntensity;
@SerializedName("declination")
@Expose
private Declination declination;
@SerializedName("inclination")
@Expose
private Inclination inclination;
@SerializedName("north-intensity")
@Expose
private NorthIntensity northIntensity;
@SerializedName("east-intensity")
@Expose
private EastIntensity eastIntensity;
@SerializedName("vertical-intensity")
@Expose
private VerticalIntensity verticalIntensity;
@SerializedName("horizontal-intensity")
@Expose
private HorizontalIntensity horizontalIntensity;

public TotalIntensity getTotalIntensity() {
return totalIntensity;
}

public void setTotalIntensity(TotalIntensity totalIntensity) {
this.totalIntensity = totalIntensity;
}

public Declination getDeclination() {
return declination;
}

public void setDeclination(Declination declination) {
this.declination = declination;
}

public Inclination getInclination() {
return inclination;
}

public void setInclination(Inclination inclination) {
this.inclination = inclination;
}

public NorthIntensity getNorthIntensity() {
return northIntensity;
}

public void setNorthIntensity(NorthIntensity northIntensity) {
this.northIntensity = northIntensity;
}

public EastIntensity getEastIntensity() {
return eastIntensity;
}

public void setEastIntensity(EastIntensity eastIntensity) {
this.eastIntensity = eastIntensity;
}

public VerticalIntensity getVerticalIntensity() {
return verticalIntensity;
}

public void setVerticalIntensity(VerticalIntensity verticalIntensity) {
this.verticalIntensity = verticalIntensity;
}

public HorizontalIntensity getHorizontalIntensity() {
return horizontalIntensity;
}

public void setHorizontalIntensity(HorizontalIntensity horizontalIntensity) {
this.horizontalIntensity = horizontalIntensity;
}

}
-----------------------------------com.example.GeomagneticFieldModelResult.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class GeomagneticFieldModelResult {

@SerializedName("model")
@Expose
private String model;
@SerializedName("model_revision")
@Expose
private String modelRevision;
@SerializedName("date")
@Expose
private Date date;
@SerializedName("coordinates")
@Expose
private Coordinates coordinates;
@SerializedName("field-value")
@Expose
private FieldValue fieldValue;
@SerializedName("secular-variation")
@Expose
private SecularVariation secularVariation;

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getModelRevision() {
return modelRevision;
}

public void setModelRevision(String modelRevision) {
this.modelRevision = modelRevision;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public Coordinates getCoordinates() {
return coordinates;
}

public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
}

public FieldValue getFieldValue() {
return fieldValue;
}

public void setFieldValue(FieldValue fieldValue) {
this.fieldValue = fieldValue;
}

public SecularVariation getSecularVariation() {
return secularVariation;
}

public void setSecularVariation(SecularVariation secularVariation) {
this.secularVariation = secularVariation;
}

}
-----------------------------------com.example.HorizontalIntensity.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class HorizontalIntensity {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.HorizontalIntensity_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class HorizontalIntensity_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.Inclination.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Inclination {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.Inclination_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Inclination_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.Latitude.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Latitude {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Double value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Double getValue() {
return value;
}

public void setValue(Double value) {
this.value = value;
}

}
-----------------------------------com.example.Longitude.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Longitude {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Double value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Double getValue() {
return value;
}

public void setValue(Double value) {
this.value = value;
}

}
-----------------------------------com.example.NorthIntensity.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NorthIntensity {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.NorthIntensity_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NorthIntensity_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.SecularVariation.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class SecularVariation {

@SerializedName("total-intensity")
@Expose
private TotalIntensity_ totalIntensity;
@SerializedName("declination")
@Expose
private Declination_ declination;
@SerializedName("inclination")
@Expose
private Inclination_ inclination;
@SerializedName("north-intensity")
@Expose
private NorthIntensity_ northIntensity;
@SerializedName("east-intensity")
@Expose
private EastIntensity_ eastIntensity;
@SerializedName("vertical-intensity")
@Expose
private VerticalIntensity_ verticalIntensity;
@SerializedName("horizontal-intensity")
@Expose
private HorizontalIntensity_ horizontalIntensity;

public TotalIntensity_ getTotalIntensity() {
return totalIntensity;
}

public void setTotalIntensity(TotalIntensity_ totalIntensity) {
this.totalIntensity = totalIntensity;
}

public Declination_ getDeclination() {
return declination;
}

public void setDeclination(Declination_ declination) {
this.declination = declination;
}

public Inclination_ getInclination() {
return inclination;
}

public void setInclination(Inclination_ inclination) {
this.inclination = inclination;
}

public NorthIntensity_ getNorthIntensity() {
return northIntensity;
}

public void setNorthIntensity(NorthIntensity_ northIntensity) {
this.northIntensity = northIntensity;
}

public EastIntensity_ getEastIntensity() {
return eastIntensity;
}

public void setEastIntensity(EastIntensity_ eastIntensity) {
this.eastIntensity = eastIntensity;
}

public VerticalIntensity_ getVerticalIntensity() {
return verticalIntensity;
}

public void setVerticalIntensity(VerticalIntensity_ verticalIntensity) {
this.verticalIntensity = verticalIntensity;
}

public HorizontalIntensity_ getHorizontalIntensity() {
return horizontalIntensity;
}

public void setHorizontalIntensity(HorizontalIntensity_ horizontalIntensity) {
this.horizontalIntensity = horizontalIntensity;
}

}
-----------------------------------com.example.TotalIntensity.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TotalIntensity {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.TotalIntensity_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TotalIntensity_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.VerticalIntensity.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class VerticalIntensity {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
-----------------------------------com.example.VerticalIntensity_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class VerticalIntensity_ {

@SerializedName("units")
@Expose
private String units;
@SerializedName("value")
@Expose
private Integer value;

public String getUnits() {
return units;
}

public void setUnits(String units) {
this.units = units;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}

So now along as the schema doesn't change this will return the values in the correct format

4 Comments

Actually you can use the same class for each property in coordinates, field-value, secular-variation
@TDIScott So I create all of these classes, do i need to create all just to return the values i need? and once i created these what do i put in my onResponse?
@HTBuild A JSON converter would automatically fill all these values for you once you have the Schema. you would see the top "Object" in your case geomagnetic-field-model-result as the returnType from your network call. So it would be public void onResponse(GeomagneticFieldModelResult response)
@MarkiianBenovskyi i agree this could be refined down into a generic class along as the naming convention doesn't change for each Object
0

You can use Jackson like this:

import com.fasterxml.jackson.databind.ObjectMapper;

        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(input);
        System.out.println(jsonNode.get("geomagnetic-field-model-result").get("field-value").get("total-intensity").get("value"));

1 Comment

The issue here is not using Jackson or not, it is that OP is missing an additional tree node geomagnetic-field-model-result when trying to get their values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.