簡體   English   中英

錯誤JSON解析數據android

[英]Error JSON parsing data android

我有一個數據解析問題,在我的logcat中,我發現了以下錯誤:

Error parsing dataorg.json.JSONException: No value for nome

這是我的解析器代碼:

try {
           JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                String aa = json_data.getString("nome");
                Log.i("log_tag", "JSON:" + aa);
            }
        }
        catch (JSONException e)
             {
            Log.e("log_tag_parsing", "Error parsing data" + e.toString());
             }

顯然,字段名稱存在於我的json結果頁面中。 為了清楚起見,我還將發布我的json。

[
    {
        "punti": {
            "_id": {
                "$id": "551fb585ecba12c819000032"
            },
            "nome": "Google",
            "loc": [
                -122.083983,
                37.422969
            ],
            "icona": 1,
            "istituzione_id": {
                "$id": "551fb556ecba12c819000031"
            }
        }
    }
]

有人可以幫我嗎? 我嘗試了很多方法,但我無法解決。 謝謝

您需要從"punti"對象中提取"nome"json_data.getJSONObject("punti")

try {
    JSONArray jArray = new JSONArray(result);
    for (int i = 0; i < jArray.length(); i++) {
        JSONObject json_data = jArray.getJSONObject(i);
        JSONObject punti = json_data.getJSONObject("punti");
        String aa = punti.getString("nome");
        Log.i("log_tag", "JSON:" + aa);
    }
} catch (JSONException e) {
    Log.e("log_tag_parsing", "Error parsing data" + e.toString());
}

您的“ nome”字段位於“ punti” JSONObject中。 您的代碼必須是:

JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                JSONObject json_punti = json_data.getJSONObject("punti");
                String aa = json_punti .getString("nome");
                Log.i("log_tag", "JSON:" + aa);
            }

暫無
暫無

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

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