簡體   English   中英

如何訪問JSONObject子字段?

[英]How do I access a JSONObject subfield?

我覺得愚蠢,但我一直在尋找這個。 我正在使用谷歌地理編碼器API,我需要一些json響應的幫助。 這是我擁有的JSONObject:

{
"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
}
}

如何將“位置”子字段拉入自己的變量? 我試過jsonObjectVariable.getString("location"); jsonObjectVariable.getDouble()等但它沒有正確返回。 你在json對象中稱為子字段是什么? 我已經讀過你可以使用object.subobject語法訪問子字段,但我只是沒有得到我需要的東西。

(我使用json-org作為庫)

謝謝您的幫助!

使用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庫不是你想要的:它不支持這種訪問。


作為一個副節點,在你的問題中給出的JSON的任何部分調用getString("location")是沒有意義的。 被稱為“location”的唯一屬性的值是另一個具有兩個屬性“lng”和“lat”的對象。

如果你想要這個“作為一個字符串”,最接近的是調用JSONObject location上的toString() (這個答案中的第一個代碼片段),這將給你類似{"lng":-78.922026,"lat":36.0083185}東西{"lng":-78.922026,"lat":36.0083185}

我相信你需要使用jsonObjectVariable.getJSONObject(“location”),后者又返回另一個JSONObject。

然后,您可以在該對象上調用getDouble(“lng”)或getDouble(“lat”)。

例如

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

您應該創建類位置以拉出“位置”子字段。

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;
    }

您可以擴展JSONObject類並覆蓋public Object get(String key) throws JSONException其中包含以下內容:

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);
}

請隨意更好地處理異常..我確定它沒有正確處理。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM