使用PHP访问第3级和第4级JSON数据

时间:2016-12-05 19:23:28

标签: php arrays json

好日子! 我有一个小问题,PHP访问json数组中的第3和第4级数据。根据下面的json数据,我有“servicelist”,从这个servicelist我有一堆信息,如服务器的IP地址和在其下运行的进程。

我的问题是如何使用php循环遍历此IP地址及其下的服务值。

json数据如下:

{
  "format_version": 0,
  "result": {
    "query_time": 1480964905000,
    "cgi": "statusjson.cgi",
    "user": "nagiosadmin",
    "query": "servicelist",
    "query_status": "released",
    "program_start": 1480936959000,
    "last_data_update": 1480964898000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "selectors": {
      "hostgroup": "Premium"
    },
    "servicelist": {
      "104.11.2.25": {
        "DISK": 2,
        "L2TP": 2,
        "LOAD": 2,
        "OPENVPN TCP": 2,
        "OPENVPN UDP": 2,
        "PING": 2,
        "PPTP": 2,
        "PROCESS": 2,
        "USERS": 2,
        "XL2TPD": 2
      },
      "104.44.13.17": {
        "DISK": 2,
        "L2TP": 2,
        "LOAD": 2,
        "OPENVPN TCP": 2,
        "OPENVPN UDP": 2,
        "PING": 4,
        "PPTP": 2,
        "PROCESS": 2,
        "USERS": 2,
        "XL2TPD": 2
      },
      "14.244.15.16": {
        "DISK": 2,
        "L2TP": 2,
        "LOAD": 2,
        "OPENVPN TCP": 2,
        "OPENVPN UDP": 2,
        "PING": 4,
        "PPTP": 2,
        "PROCESS": 2,
        "USERS": 2,
        "XL2TPD": 2
      }
   }
   }
}

我的不完整的PHP代码如下:

    <?php

$username = "thisuser";
$password = "this password";
$url = "http://nagios.services.com/nagios/cgi-bin/statusjson.cgi?query=servicelist&hostgroup=Servers&hoststatus=up+down+unreachable+pending&servicestatus=ok+warning+critical+unknown+pending";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = json_decode(curl_exec($ch));
curl_close($ch);

foreach($jsonData->data as $thisData){
    foreach($thisData->servicelist as $values)
    {
        foreach($values as $result){
            echo $result->LOAD;
        }
    }  
}

?>

基本上我想要的是将信息放在一个表中,如: Sample HTML Table

2 个答案:

答案 0 :(得分:1)

你可以试试这段代码:

<?php

     $username = "thisuser";
     $password = "this password";
     $url = "http://nagios.services.com/nagios/cgi-bin/statusjson.cgi?query=servicelist&hostgroup=Servers&hoststatus=up+down+unreachable+pending&servicestatus=ok+warning+critical+unknown+pending";

     $ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     $jsonData = json_decode(curl_exec($ch), true);
     curl_close($ch);

     foreach($jsonData['data']['servicelist'] as $ip => $service_list) {
        echo "<p>IP Address: {$ip}, Load: {$service_list['LOAD']}</p>";
    }

?>

修改

<?php

     $username = "thisuser";
     $password = "this password";
     $url = "http://nagios.services.com/nagios/cgi-bin/statusjson.cgi?query=servicelist&hostgroup=Servers&hoststatus=up+down+unreachable+pending&servicestatus=ok+warning+critical+unknown+pending";

     $ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     $jsonData = json_decode(curl_exec($ch), true);
     curl_close($ch);

     foreach($jsonData['data']['servicelist'] as $ip => $service_list) {
        echo "<p>IP Address: {$ip}, Load: {$service_list['LOAD']}</p>";
    }

    $ip_status = array(
       '1' => 'Pending',
       '2' => 'OK',
       '4' => 'Warning',
       '8' => 'Unknown',
       '16' => 'Critical'
    );

?>

<html>
  <head>
     <title>IP Data</title>
     <style>
        table {
           border-collapse: collapse;
        }

        table th, table tr td {
           text-align: center;
           border: 1px solid #ccc;
           text-transform: uppercase;
           padding: 10px 20px;
        }
    </style>
  </head>
  <body>
    <table>
      <thead>
         <th>IP Address</th>
         <th>Load</th>
         <th>OpenVPN TCP</th>
         <th>OpenVPN UDP</th>
         <th>Users</th>
         <th>L2TP</th>
         <th>PPTP</th>
         <th>XL2TPD</th>
      </thead>
      <tbody>
          <?php if(!empty($jsonData['data']['servicelist'])): ?>
              <?php foreach($jsonData['data']['servicelist'] as $ip => $service_list): ?>
                  <tr>
                     <td><?= $ip ?></td>
                     <td><?= $ip_status[$service_list['LOAD']] ?></td>
                     <td><?= $ip_status[$service_list['OPENVPN TCP']] ?></td>
                     <td><?= $ip_status[$service_list['OPENVPN UDP']] ?></td>
                     <td><?= $ip_status[$service_list['USERS']] ?></td>
                     <td><?= $ip_status[$service_list['L2TP']] ?></td>
                     <td><?= $ip_status[$service_list['PPTP']] ?></td>
                     <td><?= $ip_status[$service_list['XL2TPD']] ?></td>
                  </tr>
              <?php endforeach; ?>
          <?php endif; ?>
      </tbody>
 </table>

以下是您正在寻找的完整代码。希望它有所帮助!

答案 1 :(得分:0)

以下是如何处理数据结构的简单示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php

ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL);


$s = '{
    "format_version": 0,
    "result": {
        "query_time": 1480964905000,
        "cgi": "statusjson.cgi",
        "user": "nagiosadmin",
        "query": "servicelist",
        "query_status": "released",
        "program_start": 1480936959000,
        "last_data_update": 1480964898000,
        "type_code": 0,
        "type_text": "Success",
        "message": ""
    },
    "data": {
        "selectors": {
            "hostgroup": "Premium"
        },
        "servicelist": {
            "104.11.2.25": {
                "DISK": 2,
                "L2TP": 2,
                "LOAD": 2,
                "OPENVPN TCP": 2,
                "OPENVPN UDP": 2,
                "PING": 2,
                "PPTP": 2,
                "PROCESS": 2,
                "USERS": 2,
                "XL2TPD": 2
            },
            "104.44.13.17": {
                "DISK": 2,
                "L2TP": 2,
                "LOAD": 2,
                "OPENVPN TCP": 2,
                "OPENVPN UDP": 2,
                "PING": 4,
                "PPTP": 2,
                "PROCESS": 2,
                "USERS": 2,
                "XL2TPD": 2
            },
            "14.244.15.16": {
                "DISK": 2,
                "L2TP": 2,
                "LOAD": 2,
                "OPENVPN TCP": 2,
                "OPENVPN UDP": 2,
                "PING": 4,
                "PPTP": 2,
                "PROCESS": 2,
                "USERS": 2,
                "XL2TPD": 2
            }
        }
    }
}';

$jsonData = json_decode($s);

echo '<table border=1>';
$titles = array_keys((array)current($jsonData->data->servicelist));

echo '<tr>';
echo '<td>IP Address</td>';

foreach($titles as $col){

    echo '<td>' . $col . '</td>';
}
echo '<tr>';

foreach($jsonData->data->servicelist as $key => $data){
    echo '<tr>';
    echo "<td>$key</td>";
    foreach ($data as $prop => $val){
        echo "<td>$val</td>";
    }
    echo '</tr>';
}
echo '</table>';    
?>
</body>
</html>

结果=

enter image description here