How to access the JSONObject subfield?

I feel dumb, but I was looking for it for a while. I am working with google geocoding API and I need a little help with json answers. Here is the JSONObject that I have:

{
"viewport": {
    "southwest": {
        "lng": -78.9233749802915,
        "lat": 36.00696951970851
    },
    "northeast": {
        "lng": -78.92067701970849,
        "lat": 36.0096674802915
    }
},
"location_type": "ROOFTOP",
"location": {
    "lng": -78.922026,
    "lat": 36.0083185
}
}

How can I put the subfield "location" in my own variables? I tried jsonObjectVariable.getString("location");, jsonObjectVariable.getDouble()etc., but it didn’t return correctly. What do you call a subfield in a json object? I read that you can access subfields with object.subobject syntax, but I just don't get what I need.

(I use json-org as a library)

Thanks for the help!

+3
source share
4 answers

json.org Java , JSONObject:

JSONObject object = new JSONObject(json);
JSONObject location = object.getJSONObject("location");
double lng = location.getDouble("lng");
double lat = location.getDouble("lat");

" ", :

JSONObject object = new JSONObject(json);
double lng = object.getDouble("location.lng");
double lat = object.getDouble("location.lat");

json.org , : .


node, getString("location") JSON, . , "", , "lng" "lat".

, " ", - toString() JSONObject location ( ), - {"lng":-78.922026,"lat":36.0083185}.

+10

, jsonObjectVariable.getJSONObject( "location" ), , , JSONObject.

getDouble ( "lng" ) getDouble ( "lat" ) .

.

double lat = jsonObjectVariable.getJSONObject("location").getDouble("lat");
+4

You must create a Class Location to pull out the "location" subfields.

public class Location {
    private double lat;
    private double lng;

@JsonCreator
    public Location(@JsonProperty("lat") double lat, @JsonProperty("lng") double lng {
        this.lat = lat;
        this.lngenter code here = lng;
    }
0
source

you can extend the JSONObject class and override it public Object get(String key) throws JSONExceptionas follows:

public Object get(String key) throws JSONException {
    if (key == null) {
        throw new JSONException("Null key.");
    }

    Object object = this.opt(key);
    if (object == null) {
        if(key.contains(".")){
            object = this.getWithDotNotation(key);
        }
        else
            throw new JSONException("JSONObject[" + quote(key) + "] not found.");
    }
    return object;
}


private Object getWithDotNotation(String key) throws JSONException {
    if(key.contains(".")){
        int indexOfDot = key.indexOf(".");
        String subKey = key.substring(0, indexOfDot);
        JSONObject jsonObject = (JSONObject)this.get(subKey);
        if(jsonObject == null){
            throw new JSONException(subKey + " is null");
        }
        try{
            return jsonObject.getWithDotNotation(key.substring(indexOfDot + 1));                
        }catch(JSONException e){
            throw new JSONException(subKey + "." + e.getMessage());
        }
    }
    else
        return this.get(key);
}

Please feel free to handle exceptions better. I am sure that it is not being processed correctly. Thanks

-1
source

All Articles