How do I access a JSONObject subfield?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 12:05:31

With the json.org library for Java, you can only get at the individual properties of an object by first getting the parent JSONObject instance:

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

If you're trying to access properties using "dotted notation", like this:

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

then the json.org library isn't what you're looking for: It does not support this kind of access.


As a side node, it makes no sense calling getString("location") on any part of the JSON given in your question. The value of the only property that is called "location" is another object with two properties called "lng" and "lat".

If you want this "as a String", the closest thing is to call toString() on the JSONObject location (first code snippet in this answer) which will give you something like {"lng":-78.922026,"lat":36.0083185}.

I believe you'd need to use jsonObjectVariable.getJSONObject("location") which in turn returns another JSONObject.

You can then invoke getDouble("lng") or getDouble("lat") on that object.

E.g.

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

You should create Class Location to pull 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;
    }

you can extend the JSONObject class and override the public Object get(String key) throws JSONException with the following:

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 better handle the exceptions.. im sure it's not handled correctly. Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!