访问 JSON 运算符数组中的数组

我正在尝试访问“气候”键。我可以访问“UnitFeatures”运算符之外的所有项目。


"Location": {     

     "Units": [

               {

                     "BonusComments": "THIS IS A BONUS DEAL",

                     "CubicFootage": 125,

                     "OrderGrouping": "0000001CONTAINER",

                     "SquareFootage": 25,

                     "TotalUnits": 45,

                     "UnitFeature": {

                          "Access": "",

                          "Climate": "NON-CLIMATE",

                          "Doors": "",

                          "Elevation": "OUTSIDE",

                          "Floor": "1",

                          "Product": "CONTAINER"

                          },

                   },

                 ]

               }

我已经能够使用关联数组进行访问。我还有一个 for 循环,它将信息输出到一个表中。


$temp = "<table cellpadding='5px'>";

$temp .= "<tr><th>Unit Size</th>";

$temp .= "<th>Comments</th>";

$temp .= "<th>Unit Sq. Footage</th>";

$temp .= "<th>Units Available</th>";

$temp .= "<th>Monthly Rent</th></tr>";

for($i = 0; $i < sizeof($units) ; $i++) {

    if($units[$i]["SquareFootage"]<=100) {

        $temp .= "<tr>";

        $temp .= "<td id='row'>" . $units[$i]["UnitSize"] . "</td>";

        $temp .= "<td id='row'>" . $units[$i]["UnitFeature"]["Climate"] . "</td>";

        $temp .= "<td id='row'>" . $units[$i]["SquareFootage"] . "</td>";

        $temp .= "<td id='row'>" . $units[$i]["VacantUnits"] . "</td>";

        $temp .= "<td id='row'>$" .$units[$i]['Monthly'] . ".00</td>";

        $temp .= "</tr>";

    } 

}

$temp .= "</table>";

echo $temp;

我在每一种可能的配置中都尝试了包含: $units[$i]["UnitFeature"]["Climate"] 的行。


输出应为“非气候”或“气候”。


慕桂英3389331
浏览 143回答 2
2回答

蓝山帝景

查看API文档后,该UnitFeature对象似乎是可选的,并且可能为空。所以代码需要对此进行测试:foreach ($units as $u) {&nbsp; &nbsp; if($u["SquareFootage"]<=100) {&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'>" . $u["UnitSize"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; if (isset($u["UnitFeature"]["Climate"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'>" . $u["UnitFeature"]["Climate"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'></td>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'>" . $u["SquareFootage"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'>" . $u["VacantUnits"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "<td id='row'>$" .$u['Monthly'] . ".00</td>";&nbsp; &nbsp; &nbsp; &nbsp; $temp .= "</tr>";&nbsp; &nbsp; }&nbsp;}

宝慕林4294392

我试过你的 json 解码,json_decode但它不会。因为它返回语法错误。您可以使用json_last_error_msg(). 我还从您的 json 字符串中删除了一些“,”。您可以在jsonlint验证您的 json 字符串。现在它正在工作。<?php$jsonString = '{"Location" : {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; "Units" : [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "BonusComments": "THIS IS A BONUS DEAL",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "CubicFootage": 125,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "OrderGrouping": "0000001CONTAINER",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "SquareFootage": 25,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "TotalUnits": 45,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UnitFeature": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Access": "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Climate": "NON-CLIMATE",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Doors": "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Elevation": "OUTSIDE",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Floor": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Product": "CONTAINER"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; }}';$jsonType = json_decode($jsonString);foreach ($jsonType->Location->Units as $key => $value) {&nbsp; &nbsp; echo "KEY : ". $key."<br/>";&nbsp; &nbsp; echo $value->UnitFeature->Climate;}?>
打开App,查看更多内容
随时随地看视频慕课网APP