使用Foursquare API,它返回以下JSON提要
{“元”:{“代码”:200},“通知”:[{“类型”:“notificationTray”,“项目”:{“unreadCount”:1}},{“类型”:“消息”,“项目”:{“消息”:“确定!我们有你的@ Place XXX。您已经来过这里2次了。“}},{"type":"insights","item":{"insights":{"count":4,"items":[{"type":"pointsReward",”image“:”要签到XXX的好友的图片!“,”可分享“:false,”points“:{”https://foursquare.com/img/points/discovery-venue3.png","title":"First“:{”prefix“:”签入XXX的好友的https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First!“,"points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First Place XXX.",“可分享”:false,“points”:{“image”:{“prefix”:“Place XXX.",”points“:5},{"type":"pointsReward",”image“:”好友https://foursquare.com/img/points/category-outdoor.png","title":"First广场签到!“,”可分享“:false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category-outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Place好友签到!”,"points":6}},我想要做的是显示消息"OK!我们有你的@ Place XXX。你已经来过这里2次了。“,它嵌入在第二个"type”标签中
我习惯于使用foreach函数来获得这种值,但是使用这个函数我无法获得它。我需要专家的帮助,谢谢你的参与。
我尝试使用的代码没有成功:
$data = json_decode($response, true); // return array not object
foreach($data['notifications']['type'][0] as $item) {
echo $item['item']['message'];
}
发布于 2013-08-06 06:08:02
notifications
是一个数组,所以你不能像你想做的那样用'type'
来散列它。您应该遍历$data['notifications']
,直到找到一个"type"
属性为"message"
的对象。然后深入研究该对象的"item"
并获得它的"message"
。有关响应中notifications
字段的更多详细信息,请参阅https://developer.foursquare.com/docs/responses/notifications。
在您提供的样例JSON中,要直接访问所需的内容,可以使用$data['notifications'][1]['item']['message']
。根据您传入的API version,1
可能会发生变化,但只要您继续使用相同的版本,它就应该相对稳定。
https://stackoverflow.com/questions/18068052
复制相似问题