试图访问json导致PHP

时间:2014-05-08 12:14:29

标签: php arrays json object

我试图显示从cloudstack API接收的一些数据。当我使用JSON作为对象时,我可以显示我想要的一些数据,使用以下代码;

stdClass Object
(
[listvirtualmachinesresponse] => stdClass Object
    (
        [count] => 2
        [virtualmachine] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => bab03c9b-94e0-429f-906c-8853de306e9a
                        [name] => LinuxL1
                        [displayname] => LinuxL1
                        [account] => admin
                        [domainid] => a3121682-837b-11e3-964c-001372f94016
                        [domain] => ROOT
                        [created] => 2014-05-08T13:43:01+0200
                        [state] => Running
                        [haenable] => 
                        [zoneid] => 7a3ee2f0-540a-4232-8264-50a47a91001d
                        [zonename] => Amsterdam
                        [hostid] => 024b9ca2-4076-488e-ad13-86dfb7f2b766
                        [hostname] => ptvirt6
                        [templateid] => cf6abb42-574f-4840-886a-8662c5914166
                        [templatename] => CentOS 6.5
                        [templatedisplaytext] => good
                        [passwordenabled] => 
                        [isoid] => cf6abb42-574f-4840-886a-8662c5914166
                        [isoname] => CentOS 6.5
                        [isodisplaytext] => good
                        [serviceofferingid] => 4fc30c59-4600-44ed-b0bb-7fc48e795d90
                        [serviceofferingname] => L1
                        [cpunumber] => 1
                        [cpuspeed] => 1000
                        [memory] => 1024
                        [cpuused] => 0.02%
                        [networkkbsread] => 55
                        [networkkbswrite] => 1
                        [diskkbsread] => 1000680
                        [diskkbswrite] => 1301501
                        [diskioread] => 77192
                        [diskiowrite] => 8884
                        [guestosid] => a92508f4-837b-11e3-964c-001372f94016
                        [rootdeviceid] => 0
                        [rootdevicetype] => ROOT
                        [securitygroup] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => edfe848c-837b-11e3-964c-001372f94016
                                        [name] => default
                                        [description] => Default Security Group
                                        [account] => admin
                                        [ingressrule] => Array
                                            (
                                            )

                                        [egressrule] => Array
                                            (
                                            )

                                        [tags] => Array
                                            (
                                            )

                                    )

                            )

                        [nic] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => bfaeb76d-7b5a-46b1-aaa7-7dd15e86bf2e
                                        [networkid] => 83007a42-e4b7-4ea2-aa21-  20938ea3bc56  
                                        [networkname] => defaultGuestNetwork
                                        [netmask] => 111.111.111.111
                                        [gateway] => 111.111.111.111
                                        [ipaddress] => 111.111.111.111
                                        [isolationuri] => ec2://untagged
                                        [broadcasturi] => vlan://untagged
                                        [traffictype] => Guest
                                        [type] => Shared
                                        [isdefault] => 1
                                        [macaddress] => 06:a5:24:00:00:0a
                                    )

                            )

                        [hypervisor] => KVM
                        [instancename] => i-2-9-VM
                        [tags] => Array
                            (
                            )

                        [affinitygroup] => Array
                            (
                            )

                        [displayvm] => 1
                        [isdynamicallyscalable] => 
                    )

我可以使用foreach循环从列表中读取名称/状态

<?php
$api_content = file_get_contents("http://127.0.0.1:5950/client/api?command=listVirtualMachines&domainid=1&account=admin&response=json");
$api_data = json_decode($api_content);
$ap_machine = $api_data->listvirtualmachinesresponse->virtualmachine;
echo "<pre>";
print_r($api_data);
echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
      <table border="1" width="300">
    <tr>
        <td>Naam</td>
        <td>Status</td>
        <td>Ip Address</td>
        <td>Health</td>
        </tr>
    <?php 
       echo "<tr>";
       foreach($ap_machine as $item){

       echo "<td>".$item->name."</td>";
       echo "<td>".$item->state."</td>";
       echo "<td>".$item->nic->ipaddress."</td>";
       if($item->state == 'Running'){
           echo '<td><img src="/600px-Approve_icon.svg.png" height="50" width="50">      </td>';
       }
       else{
           echo '<td><img src="/fail.png" height="25" width="25"></td>';
       }
      echo "</tr>";
       }

    ?>

我已经在线阅读从json列表中创建一个关联数组,我也是这样做的,并没有得到结果。

2 个答案:

答案 0 :(得分:0)

在您的PHP代码中,您正在访问nice密钥作为json的常规元素:

echo "<td>".$item->nic->ipaddress."</td>";

但是,从你的var_dump输出我可以看到它的数组类型。

enter image description here

因此,要访问nice数组的元素,您必须通过指定数组索引来访问它。

echo "<td>".$item->nic[0]->ipaddress."</td>";

答案 1 :(得分:0)

看起来老兄

更改

  $api_data = json_decode($api_content);

  $api_data = json_decode($api_content, TRUE);

这使它成为一个关联数组

检查http://www.php.net/manual/en/function.json-decode.php