Saya mengambil Objek JSON melalui kelas Volley di Android. Mendapatkan permintaan bekerja dengan baik. Namun, saya benar-benar berjuang untuk mengurai objek raksasa ini. Di PHP, loop terlihat seperti:

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

Berdasarkan JSON saya, itu memberi saya 3 set total data.

Cuplikan JSON yang saya ambil dari:

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

Saya tidak terlalu mendalami Obyek JSON, dan saya tidak yakin bagaimana cara mendapatkan indeks 0 di dua larik tersebut (seperti yang Anda lihat dalam contoh PHP. Sejauh ini, saya telah turun sepenuhnya ke tingkat pool, tetapi tidak lebih jauh.

Bagian onResponse dari permintaan Volley saya:

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

Ini memberi saya:

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

Representasi visual dari apa yang saya butuhkan terlihat seperti ini:

enter image description here

Pada dasarnya saya perlu sampai ke pool, dan 3 string dari masing-masing. Bagaimana cara saya mencapai ini? Itu baik-baik saja di PHP, tapi astaga ini telah menjadi lubang kelinci, dan saya berjuang untuk mendapatkan informasi ini di layar aplikasi saya.

0
DevOpsSauce 20 November 2020, 08:28

1 menjawab

Jawaban Terbaik

Saya akan membuat variabel anggota string ini, tetapi untuk saat ini, saya membuatnya dalam metode onResponse saya. Aku hampir sampai. Saya hanya perlu mengulangi JSONArray terakhir untuk mendapatkan nilainya.

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

Hasil ini (bidang alamat dan label telah diedit):

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
0
IRGeekSauce 20 November 2020, 16:58