2 回答
TA贡献1843条经验 获得超7个赞
查看API文档后,该UnitFeature对象似乎是可选的,并且可能为空。所以代码需要对此进行测试:
foreach ($units as $u) {
if($u["SquareFootage"]<=100) {
$temp .= "<tr>";
$temp .= "<td id='row'>" . $u["UnitSize"] . "</td>";
if (isset($u["UnitFeature"]["Climate"])) {
$temp .= "<td id='row'>" . $u["UnitFeature"]["Climate"] . "</td>";
} else {
$temp .= "<td id='row'></td>";
}
$temp .= "<td id='row'>" . $u["SquareFootage"] . "</td>";
$temp .= "<td id='row'>" . $u["VacantUnits"] . "</td>";
$temp .= "<td id='row'>$" .$u['Monthly'] . ".00</td>";
$temp .= "</tr>";
}
}
TA贡献2021条经验 获得超8个赞
我试过你的 json 解码,json_decode但它不会。因为它返回语法错误。您可以使用json_last_error_msg(). 我还从您的 json 字符串中删除了一些“,”。您可以在jsonlint验证您的 json 字符串。现在它正在工作。
<?php
$jsonString = '{"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"
}
}
]
}}';
$jsonType = json_decode($jsonString);
foreach ($jsonType->Location->Units as $key => $value) {
echo "KEY : ". $key."<br/>";
echo $value->UnitFeature->Climate;
}
?>
- 2 回答
- 0 关注
- 142 浏览
添加回答
举报