首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用php解析foursquare json提要

使用php解析foursquare json提要
EN

Stack Overflow用户
提问于 2013-08-06 05:28:05
回答 1查看 651关注 0票数 0

使用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函数来获得这种值,但是使用这个函数我无法获得它。我需要专家的帮助,谢谢你的参与。

我尝试使用的代码没有成功:

代码语言:javascript
运行
复制
$data = json_decode($response, true); // return array not object
      foreach($data['notifications']['type'][0] as $item) {
      echo $item['item']['message']; 
}
EN

回答 1

Stack Overflow用户

发布于 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 version1可能会发生变化,但只要您继续使用相同的版本,它就应该相对稳定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18068052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档