【问题标题】:parse foursquare json feed using php使用php解析foursquare json feed
【发布时间】:2013-08-05 21:28:04
【问题描述】:

使用 Foursquare API,它会返回以下 JSON 提要

{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":1}},{"type":"message ","item":{"message":"好的!我们有你@Place XXX。你来过这里两次。"}},{"type":"insights","item":{"见解":{"count":4,"items":[{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title ":"你的朋友第一个签到地方 XXX!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-场地3_","sizes":[44,60,120],"name":".png"},"message":"你的朋友第一个入住地点 XXX!","points":5}},{" type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"第一次在地方XXX。","shareable":false, "points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png" },"message":"第一次到地方 XXX。","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/category -outdoor.png","ti tle":"朋友第一广场签到!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category- Outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Plaza 签到你的朋友!","points":6}},{"type ":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"你的第一个朋友在这里签到。","shareable":错误,“点”:{“图像”:{“前缀”:“https://foursquare.com/img/points/discovery-venue3_”,“大小”:[44,60,120],“名称”:“。 png"},"message":"你的第一个朋友在这里签到。","points":2}}]}}},{"type":"leaderboard","item":{"total": 18,"scores":[{"icon":"https://foursquare.com/img/points/discovery-venue3.png","message":"你的朋友第一个签到... 我想做的是显示消息“好的!我们有你@Place XXX。你来过这里两次。”它嵌入在第二个“类型”标签中

我习惯于使用 foreach 函数来获得这种值,但使用这个我无法获得它。 我需要专家的帮助,感谢您的参与。

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

$data = json_decode($response, true); // return array not object
      foreach($data['notifications']['type'][0] as $item) {
      echo $item['item']['message']; 
}

【问题讨论】:

  • 数据不是有效的 json。或者你错过了一些部分?

标签: php json foursquare


【解决方案1】:

notifications 是一个数组,所以你不能像你试图做的那样用'type' 散列到它里面。您应该遍历$data['notifications'],直到找到具有"type" 属性为"message" 的对象。然后挖掘该对象的"item" 并获取它的"message"。有关响应中notifications 字段的更多详细信息,请参阅https://developer.foursquare.com/docs/responses/notifications

在您提供的示例 JSON 中,要直接访问您想要的内容,您可以使用$data['notifications'][1]['item']['message']1 可能会根据您传入的API version 而发生变化,但只要您继续使用相同的版本,它就应该相对稳定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多