PHP解码嵌套JSON [英] PHP decode nested JSON

查看:121
本文介绍了PHP解码嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a nested JSON code as (it's actually my facebook status updates)

{
   "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"
               }
            ],

         }
      }
]

现在,如何访问特定更新的注释,打印它通过一个循环?(我正在同时检索几个更新)。

Now how do I access the comments for a particular update and print it through a loop?? (I'm retrieving say a couple of updates at the same time).

推荐答案

使用< a href =http://nl2.php.net/manual/en/function.json-decode.php =noreferrer> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆