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

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

感谢您的浏览。

最佳答案

根据您的 JSON,您的“lineStatuses”是一个数组。因此,您需要从“line”对象中获取数组。然后逐步遍历该数组以获得单独的“statusSeverityDescription”值。

因此,将其添加到 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", "");
    }

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

关于java - 使用 Volley 库访问 JSON 数组的嵌套项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47045679/

相关文章:

java - Flutter build apk 命令抛出错误并且未构建 apk

android - 使用 GSON 调整 Retrofit 响应

java - android开发View.OnClickListener报错

javascript - 如何使用 Mustache 迭代 JSON 数组

java - 当用户访问webapp的root时如何自动触发Struts2中的操作?

java - 客户端的 pako 和服务器端的 java

java - 在 Jenkins 的文件级别强制覆盖

java - Android Studio Logcat 没有显示任何内容

java - 我应该如何读取带有编号对象的 JSON?

java - 使用 Jackson 反序列化要列出的重复键