我正在使用Yelp API v1,我试图获得存储在类别对象中的category_filter。我的JSON的一小部分看起来像这样:
"businesses": [
{
"address1": "800 N Point St",
"phone": "4157492060",
"categories": [{
"category_filter": "newamerican",
"search_url": "http://www.yelp.com/search?cflt=newamericanu0026find_desc=u0026find_loc=800+N+Point+St%2C+San+Francisco+94109",
"name": "American (New)"
}],
"name": "Gary Danko",
}]
我的php脚本如下:
<?php
$yelpstring = file_get_contents("PATH TO JSON", true);
$obj = json_decode($yelpstring, true);
foreach($obj['businesses'] as $business){
echo "Restaurant name: " . $business['name']."<br/>";
echo "Restaurant Type: " . $business['categories']['category_filter']."<br/>";
echo "Address 1: " . $business['address1']."<br/>";
echo "Phone: " . $business['phone'] ."<br/>";
echo "<hr>";
}
?>
我得到的名称,address1和电话,但不是category_filter我得到一个php错误,说:
"注意:file.php第12行未定义索引:category_filter "
我不明白我做错了什么
$business['categories']['category_filter']
你必须使用:$business['categories'][0]['category_filter']
在你的json是"[{"这意味着有一个数组(/object,解码为关联数组)在另一个数组。