由于字段不同,在PHP中显示JSON数据时出错

时间:2015-10-16 07:08:57

标签: php json parsing field

我正在尝试使用JSON API提要在PHP中显示一个列表,并且字段不断从一个数组更改为下一个数组。

有人可以帮助解决方案以使显示正确

Json脚本,你会看到JSON在clientId下的每个实例中都有uniqueId和mac字段都有变化

    {
  "searchMeta": {
    "maxResults": 100,
    "sourceId": "6e5c1d5d-d84b-4b64-9d2c-a32b8c9f7174",
    "iso8601Timestamps": true,
    "sourceType": "VENUE",
    "endTimestamp": 1444952156,
    "startTimestamp": 1444952056
  },
  "ranges": [
    {
      "clients": [
        {
          "clientId": {
            "mac": "6c:19:8f:bf:47:e9"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -93.3
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "Kn04"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -75.3
            }
          ]
        },
        {
          "clientId": {
            "mac": "58:6d:8f:75:95:0e"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -86.2
            }
          ]
        },
        {
          "clientId": {
            "mac": "44:d9:e7:21:e0:de"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -25.8
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:26"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -47
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:29"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -72.3
            }
          ]
        },
        {
          "clientId": {
            "mac": "a4:ee:57:2e:ac:bd"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -95
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "CQos"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -64.1
            }
          ]
        },
        {
          "clientId": {
            "mac": "86:8f:c2:8f:c3:20"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -68.4
            }
          ]
        },
        {
          "clientId": {
            "mac": "32:91:8f:6c:2e:f4"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -87.7
            }
          ]
        },
        {
          "clientId": {
            "mac": "30:91:8f:6c:2e:f3"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -86.9
            }
          ]
        },
        {
          "clientId": {
            "mac": "30:91:8f:43:ca:49"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -87
            }
          ]
        },
        {
          "clientId": {
            "mac": "1d:8b:90:7b:20:9c"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -102.5
            }
          ]
        },
        {
          "clientId": {
            "mac": "38:2c:4a:5c:b6:a0"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -76.7
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "ECgg"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -59.5
            }
          ]
        }
      ],
      "timestamp": "2015-10-15T23:35:00+00:00"
    }
  ]
}

然后读取和显示它的代码是

    $json = json_decode($response,true);
    $output = "<ul>";
    foreach($json['ranges'] as $range){
        foreach($range['clients'] as $client)
        foreach($client['clientId'] as $client1)

            foreach($client['rssis'] as $data){

        $output .= "<h4>".$range['timestamp']."</h4>";

        $output .= "<li>Source ID: ".$data["sourceId"]."</li>";
        $output .= "<li>Value in dB: ".$data["value"]."</li>";
    }}

    $output .= "</ul>";


if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $output;
}

然后显示在网页上,省略uniqueId或mac字段。我需要显示其中任何一个的行吗?

  

2015-10-13T04:26:00 + 00:00

     

来源ID:zR1L3以dB为单位的值:-89.2

     

2015-10-13T04:26:00 + 00:00

     

来源ID:zR1L3以dB为单位的值:-98.8

     

2015-10-13T04:26:00 + 00:00

     

来源ID:zR1L3以dB为单位的值:-74.6

感谢下面的回答,现在我得到以下内容。

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

2015-10-16T10:11:00 + 00:00

mac:f4:b7:e2:31:65:8f

sourceId:zR1L3

值:-95

2015-10-16T10:12:00 + 00:00

2015-10-16T10:12:00 + 00:00

2015-10-16T10:12:00 + 00:00

2015-10-16T10:12:00 + 00:00

2015-10-16T10:12:00 + 00:00

mac:f4:b7:e2:31:65:8f

sourceId:zR1L3

值:-95

2015-10-16T10:13:00 + 00:00

1 个答案:

答案 0 :(得分:1)

没有得到你究竟在问什么。如果我理解正确,你想从json编码数组访问几个字段。

试试这个

$json = json_decode($response,true);
foreach($json->ranges as $row){
        foreach($row->clients as $row1)
            echo $row->timestamp."<br>";
            foreach($row1->clientId as $key => $val){
                echo $key." ==== ". $val."<br>";
            }
            foreach($row1->rssis as $row2){
                foreach($row2 as $key => $val){
                    echo $key." ==== ". $val."<br>";
                }
            }     
    }

我无法从你发布的json数组中得到时间戳。