PHP 解码嵌套的 JSON

标签 php json facebook

我有一个嵌套的 JSON 代码作为(它实际上是我的 facebook 状态更新)

{
   "data": [
      {
         "id": "1290561400000000",
         "from": {
            "name": "My name",
            "id": "500920000"
         },
         "message": "Message body",
         "updated_time": "2010-08-24T08:22:13+0000",
         "comments": {
            "data": [
               {
                  "id": "129056140474641_8000",
                  "from": {
                     "name": "name1",
                     "id": "100000486072000"
                  },
                  "message": "hahahahahahha..........",
                  "created_time": "2010-08-24T08:40:39+0000"
               },
               {
                  "id": "129056140474641_8000000",
                  "from": {
                     "name": "name2",
                     "id": "1597542457"
                  },
                  "message": "true ya. I have updated",
                  "created_time": "2010-08-24T08:59:53+0000"
               },
               {
                  "id": "129056140474641_83000",
                  "from": {
                     "name": "Name3",
                     "id": "1000004860700000"
                  },
                  "message": "am putting it on my wall....",
                  "created_time": "2010-08-24T09:01:25+0000"
               }
            ],

         }
      }
]

现在如何访问特定更新的评论并通过循环打印它?? (我正在同时检索说几个更新)。

最佳答案

使用json_decode() :

$decoded = json_decode($json_string);
$comments = $decoded->data[0]->comments->data;
foreach($comments as $comment){
   $name = $comment->from->name;
   $message = $comment->message;
   //do something with it
}

关于PHP 解码嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555335/

相关文章:

objective-c - 错误 : "Calls to mailbox_fql have exceeded the rate of 300 calls per 600 seconds"

php - 通过字符串调用 Laravel 模型

JSON 文档索引失败

facebook - 如何使用 xamarin.auth 登录 facebook 并以 xamarin 形式获取用户的个人资料信息

java - 在字符串之间搜索文本 android

javascript - 如何从 foursquare field 提示 api url 获取 JSON 数据并以 HTML 格式制作

java - 无法获取 Facebook ID 和电子邮件

javascript - Laravel 无法使用模态和 ajax 的增量方法添加数量

PHPUnit 检查期望在触发后带有参数

javascript - 如何在文本框值中输入当前日期并显示我的附加数据