我有一个嵌套的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/

10-11 02:20