How can I extract data from JSON?

Vlad

I am calling a GET request to an api which should return an JSON with longitude, latitude and addresses.

I am trying to get the addresses into an ArrayList and place them into a Spinner. I successfully retrieve the whole JSON but when I try to take only the addresses I get this error:

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)

The code that is causing the problem is:

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




    }
}

EDIT: This is the whole JSON I am expecting:

{
    "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"]
}
Alvaro De la Cruz

You're trying to get a json object from string array.

Instead of this:

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

Try this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract this data from JSON from API in Python?

how can I extract data from Json which start from "["?

How can I extract url from json data with escaped "\" in java

How can i extract data from this line?

How can i extract data from String?

How can I extract the data from these strings?

How can I extract 2-depth json data from my server in ruby?

How can i use ijson to extract a set of corresponding data from json file?

Why can I not extract the data from this basic JSON Example

Can I extract JSON data from this XHR request?

How I can extract object from JSON result usinig angularJS

How can I extract uniqued caterogies from JSON in Flutter?

How can I extract Map from my JSON respone

How can I extract a JSON object from a CSV file in Python?

How can I extract specific value from JSON response?

How can I extract response from json array in Karate

How can I extract one value from this JSON ARRAY?

How can I extract these key/value pairs from this JSON node?

How can i extract the value of "token" from this JSON response with Java

How can I extract one value from JSON Lambda output?

how can i Extract JSON from HTTP Response (python)

How can I extract the Matrix data from a mesh?

How can I extract data from a list of parent objects with children?

How can I extract alternative names data from a CSR?

How can I extract data from a dictionary by specifying a range of values?

How can I extract data from dat file?

How can I extract data from text file?

How can I extract data from all elements of a multiset?

How can I extract selective data from a webpage using rvest?