如何从JSON提取数据?

时间:2018-12-11 15:53:41

标签: android json

我正在向api调用GET请求,该请求应返回具有经度,纬度和地址的JSON。

我正在尝试将地址放入ArrayList并将其放入Spinner。 我成功检索了整个JSON,但是当我尝试仅接收地址时,出现此错误:

W/System.err: org.json.JSONException: Value 1 Chester Street, , , , , 
Coventry, West Midlands at 0 of type java.lang.String cannot be converted to 
JSONObject
    at org.json.JSON.typeMismatch(JSON.java:101)
W/System.err:     at org.json.JSONArray.getJSONObject(JSONArray.java:525)
    at com.example.vladu.carpark.LocationActivity$getJsonData.onPostExecute(LocationActivity.java:139)
    at com.example.vladu.carpark.LocationActivity$getJsonData.onPostExecute(LocationActivity.java:93)
    at android.os.AsyncTask.finish(AsyncTask.java:695)
    at android.os.AsyncTask.access$600(AsyncTask.java:180)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

引起问题的代码是:

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        if (pd.isShowing()){
            pd.dismiss();
        }
        try{
            jsonObject = new JSONObject(response);
            jsonArray = jsonObject.getJSONArray("addresses");
            //jsonObject = jsonArray.getJSONObject(0);

            for(int i = 0; i < jsonArray.length(); i++){
                jsonObject = jsonArray.getJSONObject(i);
                addresses.add(jsonObject.toString());
            }

            Spinner addressSpinner = (Spinner) findViewById(R.id.addressSpinner);

            addressSpinner.setAdapter(new ArrayAdapter<String>(LocationActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,addresses));


        }catch(JSONException e){
            e.printStackTrace();
        }




    }
}

编辑: 这是我期望的整个JSON:

{
    "latitude": 52.24593734741211,
    "longitude": -0.891636312007904,
    "addresses":
    ["10 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "12 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "14 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "16 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "18 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "2 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "20 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "22 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "24 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "26 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "26a Watkin Terrace, , , , , Northampton, Northamptonshire",
    "26b Watkin Terrace, , , , , Northampton, Northamptonshire",
    "26c Watkin Terrace, , , , , Northampton, Northamptonshire",
    "26d Watkin Terrace, , , , , Northampton, Northamptonshire",
    "28 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "2a Watkin Terrace, , , , , Northampton, Northamptonshire",
    "30 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "32 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "36 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "38 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "4 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "40 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "40b Watkin Terrace, , , , , Northampton, Northamptonshire",
    "42 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "44 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "46 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "48 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "50 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "8 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "Flat 1, 6 Watkin Terrace, , , , Northampton, Northamptonshire",
    "Flat 1, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 2, 6 Watkin Terrace, , , , Northampton, Northamptonshire",
    "Flat 2, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 3, 6 Watkin Terrace, , , , Northampton, Northamptonshire",
    "Flat 3, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 4, 6 Watkin Terrace, , , , Northampton, Northamptonshire",
    "Flat 4, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 5, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 6, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 7, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 8, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire",
    "Flat 9, Watkin Court, Watkin Terrace, , , Northampton, Northamptonshire"]
}

1 个答案:

答案 0 :(得分:1)

您正在尝试从字符串数组中获取json对象。

代替此:

    for(int i = 0; i < jsonArray.length(); i++){
        jsonObject = jsonArray.getJSONObject(i);
        addresses.add(jsonObject.toString());
    }

尝试一下:

    for(int i = 0; i < jsonArray.length(); i++){
        String spinnerElement =  jsonArray.getString(i);
        addresses.add(spinnerElement);
    }