使用Volley库访问JSON数组的嵌套项

时间:2017-10-31 22:53:57

标签: java android json android-volley

我正在尝试使用Volley库从嵌套的JSON数组中访问信息。具体来说,我试图从'lineStatuses'中获取'statusSeverityDescription'的属性。我已经搜索并尝试了许多我见过的解决方案。似乎无法获得我需要的数据。请参阅下面的代码和JSON。

代码

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

感谢您的光临。

1 个答案:

答案 0 :(得分:3)

根据你的JSON你的&#34; lineStatuses&#34;是一个数组。所以你需要从你的&#34;线&#34;宾语。然后单步执行该数组以获取个人&#34; statusSeverityDescription&#34;值。

请将此添加到for循环

中的代码中
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", "");
    }

假设您的代码没有其他问题(您没有提及任何错误),上述代码应该可以正常工作。