使用 Android VOLLEY 迭代和解析嵌套的 JSON 对象

Iterate and parse nested JSON Objects with Android VOLLEY

我正在通过 Android 中的 Volley class 检索 JSON 对象。获得请求效果很好。但是,我真的很难解析这个巨大的对象。在 PHP 中,循环类似于:

foreach($json['data'][0]['region'][0]['pool'] as $item) { 
   $serve_mode = $item['serve_mode'];
   $address = $item['address'];
   $label = $item['label']; 
}

根据我的 JSON,总共有 3 组数据。

我抓取的JSON的片段:

{
    "data": {
        "active": "Y",
        "auto_recover": "Y",
        "contact_nickname": "owner",
        "fqdn": "example.com",
        "monitor": {
            "expected": "",
            "header": "",
            "host": "www.example.com",
            "interval": 5,
            "path": "/hello.php",
            "port": 0,
            "protocol": "HTTPS",
            "retries": 3,
            "timeout": 0
        },
        "notify_events": "svc,ip,nosrv",
        "recovery_delay": 0,
        "region": [
            {
                "failover_data": "12.345.678.90",
                "failover_mode": "ip",
                "min_healthy": 1,
                "pool": [
                    {
                        "address": "67.890.123.45",
                        "label": "SomeCompany Circuit A",
                        "log": [
                            {
                                "message": "",
                                "site_code": "DFW",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "IAD",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "MIA",
                                "status": "up",
                                "time": 1605843120
                            }
                        ],
                        "serve_mode": "obey",
                        "status": "up",
                        "weight": 2
                    },
                    {
                        "address": "34.567.89.012",
                        "label": "SomeCompany Circuit B",
                        "log": [
                            {
                                "message": "",
                                "site_code": "DFW",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "IAD",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "MIA",
                                "status": "up",
                                "time": 1605843120
                            }
                        ],
                        "serve_mode": "obey",
                        "status": "up",
                        "weight": 1
                    },
                    {
                        "address": "12.345.678.90",
                        "label": "SomeCompany Circuit C",
                        "log": [
                            {
                                "message": "",
                                "site_code": "MIA",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "DFW",
                                "status": "up",
                                "time": 1605843120
                            },
                            {
                                "message": "",
                                "site_code": "IAD",
                                "status": "up",
                                "time": 1605843120
                            }
                        ],
                        "serve_mode": "obey",
                        "status": "up",
                        "weight": 2
                    }
                ],
                "region_code": "global",
                "serve_count": 1
            }
        ],

我对 JSON 对象的了解还不够深入,我不确定如何在这两个数组中获取索引 0(正如您在 PHP 示例。到目前为止,我已经一路下降到 pool 级别,但没有进一步。

我的 Volley 请求的 onResponse 部分:

    @Override
    public void onResponse(JSONObject response) {
      try {
        JSONObject data = response.getJSONObject("data");
        JSONArray region = data.getJSONArray("region");
        JSONObject pool = region.getJSONObject(0);
        JSONArray item = pool.getJSONArray("pool");
        
        Log.d(TAG, "item: "+item);

      } catch (JSONException e) {
          e.printStackTrace();
          Log.d(TAG, "JSONException: "+e.toString());
    }
   
  }
}

它给了我:

[{
    "status": "up",
    "log": [{
        "status": "up",
        "message": "",
        "site_code": "DFW",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "IAD",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "MIA",
        "time": 1605848520
    }],
    "weight": 2,
    "serve_mode": "obey",
    "address": "67.890.123.45",
    "label": "SomeCompany Circuit A"
}, {
    "status": "up",
    "log": [{
        "status": "up",
        "message": "",
        "site_code": "DFW",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "IAD",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "MIA",
        "time": 1605848520
    }],
    "weight": 1,
    "serve_mode": "obey",
    "address": "34.567.890.12",
    "label": "SomeCompany Circuit B"
}, {
    "status": "up",
    "log": [{
        "status": "up",
        "message": "",
        "site_code": "MIA",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "DFW",
        "time": 1605848520
    }, {
        "status": "up",
        "message": "",
        "site_code": "IAD",
        "time": 1605848520
    }],
    "weight": 2,
    "serve_mode": "obey",
    "address": "12.234.567.890",
    "label": "SomeCompany Circuit C"
}]

我需要的视觉表示如下所示:

基本上我需要一直到池子,每个字符串都有 3 个字符串。我到底如何实现这一目标? PHP 没问题,但是天哪,这已经变成了一个兔子洞,我正在努力在我的应用程序屏幕上获取这些信息。

我打算让这些字符串成为成员变量,但现在,我是在我的 onResponse 方法中创建它们的。我快到了。我只需要遍历最终的 JSONArray 来获取值。

String serve_mode = "";
String address = "";
String label = "";
String weight = "";
try {
  JSONObject data = response.getJSONObject("data");
  JSONArray region = data.getJSONArray("region");
  JSONObject pool = region.getJSONObject(0);
  JSONArray item = pool.getJSONArray("pool");

  //NOW ITERATE OVER THE NESTED ARRAY "pool"
  for(int i = 0; i < item.length(); i++) {
    JSONObject pool_item = item.getJSONObject(i);
    serve_mode = pool_item.getString("serve_mode");
    address = pool_item.getString("address");
    label = pool_item.getString("label");
    weight = pool_item.getString("weight");

    Log.d(TAG, "serve_mode: "+serve_mode);
    Log.d(TAG, "address: "+address);
    Log.d(TAG, "label: "+label);
    Log.d(TAG, "weight: "+weight);
  }
} catch (JSONException e) {
    Log.d(TAG, "JSONException: "+e.toString());
  }

这会产生(地址和标签字段已编辑):

D/MainActivity: serve_mode: obey
D/MainActivity: address: 12.345.678.90
D/MainActivity: label: SomeCompany A
D/MainActivity: weight: 2
D/MainActivity: serve_mode: obey
D/MainActivity: address: 123.456.789.01
D/MainActivity: label: SomeCompany B
D/MainActivity: weight: 1
D/MainActivity: serve_mode: obey
D/MainActivity: address: 456.789.012.34
D/MainActivity: label: SomeCompany C
D/MainActivity: weight: 2