astric mobiles
astric mobiles

Reputation: 476

Access nested Items of a JSON array using Volley library

I'm trying to access information from within a nested JSON array using the Volley library. Specifically, I am trying to get the properties of 'statusSeverityDescription' from within 'lineStatuses'. I've searched for and tried many solutions I have seen. Just cant seem to get the data i need. Please see code and JSON below.

Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bob = (TextView) findViewById(R.id.bob);
    tempTextView = (TextView) findViewById(R.id.tempTextView);

    String url = "https://api.tfl.gov.uk/Line/victoria/Status";

     JsonArrayRequest jsArrayRequest = new JsonArrayRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {

                    try{
                        // Loop through the array elements
                        for(int i=0;i<response.length();i++){
                            // Get current json object
                            JSONObject line = response.getJSONObject(i);

                            // Get the current line (json object) data
                            String lineName = line.getString("name");

                            String lineStatus = line.getString("lineStatuses");

                            // Display the formatted json data in text view

                            tempTextView.setText(lineName);

                            bob.setText(lineStatus);

                            Log.v("status", "Response: " + lineStatus);

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






             new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("TAG", "Error response:", error);
                }
            });

JSON

[
{
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
    "id": "northern",
    "name": "Northern",
    "modeName": "tube",
    "disruptions": [],
    "created": "2017-10-31T10:48:22.99Z",
    "modified": "2017-10-31T10:48:22.99Z",
    "lineStatuses": [
        {
            "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
            "id": 0,
            "statusSeverity": 10,
            "statusSeverityDescription": "Good Service",
            "created": "0001-01-01T00:00:00",
            "validityPeriods": []
        }
    ],
    "routeSections": [],
    "serviceTypes": [
        {
            "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
            "name": "Regular",
            "uri": "/Line/Route?ids=Northern&serviceTypes=Regular"
        },
        {
            "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
            "name": "Night",
            "uri": "/Line/Route?ids=Northern&serviceTypes=Night"
        }
    ],
    "crowding": {
        "$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
    }
}
   ]

Thanks for looking.

Upvotes: 0

Views: 579

Answers (1)

Barns
Barns

Reputation: 4848

According to your JSON your "lineStatuses" is an array. So you need to get the array from your "line" object. Then step through that array in order to get the individual "statusSeverityDescription" values.

So add this to your code in your for loop

JSONArray arrayStatus = line.getJSONArray("lineStatuses");
int len = arrayStatus.length();
    for (int j = 0; j < len; j++) {
        JSONObject o = arrayStatus.getJSONObject(j);
        String statusSeverityDescription = o.optString("statusSeverityDescription", "");
    }

Assuming you have no other issues with your code (you did not mention any errors) the above code should work.

Upvotes: 3

Related Questions