我一直在试图解析一个没有成功的json数组。我可以得到一个根元素,但没有任何数组元素。下面是我的Foursquare的json数组的开始,它有重新发生的场地元素。解析PHP中的json数组
response: {
keywords: {}
suggestedRadius: 10000
headerLocation: "here"
headerFullLocation: "here"
headerLocationGranularity: "unknown"
headerMessage: "Suggestions for Friday evening"
totalResults: 214
groups: [
{
type: "Recommended Places"
name: "recommended"
items: [
{
reasons: {
count: 0
items: [ ]
}
venue: {
id: "4b799a05f964a520b1042fe3"
name: "Green Gables"
contact: {
phone: "3097472496"
formattedPhone: "(309) 747-2496"
}
location: {
address: "17485 East 2500 North Rd"
以下是我尝试获取餐厅名称的PHP代码。
$obj = json_decode($uri, true);
foreach($obj['response']['groups'] as $p)
{
if(isset($p['items']))
{
foreach($p['items'] as $p1)
{
if(isset($p1['venue']))
{
// echo varDumpToString($p1['venue']); // this dump works ok and shows the elements
foreach($p1['venue'] as $p2)
{
echo varDumpToString($p2['name']); // This is where I get the error
}
}
}
}
}
程序向下钻取到name元素,然后给了我一个错误说“Unefined指数:名称”,并“非法串偏移名”此消息出现14次这是一次对每个项目该数组。那么,为什么是“名”不承认?
当您使用json_decode()解码的JSON,你回吧作为一个数组或对象? 如果是对象,请尝试使用$ p2-> name。 – Adrian
你确定节点转换为'array',而不是'stdObject'吗?在这种情况下,您可以通过'$ p2-> name'访问名称 – thaJeztah
LOL @AdrianCrepaz击败了我 – thaJeztah