-1

以前关于 SO 的类似问题给了我线索,但由于我的情况是 json 格式,我有问题。我使用解析 $url 的内容file_get_content($url)

这给了我结果json字符串:

{
   "http://ec.x.x.x.xcompute-1.amazonaws.com/": {
      "comments": {
         "data": [
            {
               "id": "239_320054",
               "from": {
                  "name": "x",
                  "id": "46353"
               },
               "message": "testing",
               "can_remove": false,
               "created_time": "2013-10-02T10:47:30+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "181319910",
               "from": {
                  "name": "y",
                  "id": "166353"
               },
               "message": "hi",
               "can_remove": false,
               "created_time": "2013-10-02T07:30:00+0000",
               "like_count": 0,
               "user_likes": false
            }
         ],
         "paging": {
            "cursors": {
               "after": "MQ==",
               "before": "Mg=="
            }
         }
      }
   }
}

我的问题:如何从上面的 json 字符串中解析单个字符串中的所有消息?

根据我从其他 SO 答案和参考中获得的线索,我可以这样做:

$json = "above_json_result";
$obj = json_decode($json);
print $obj->{'message'};

这是我的整个代码,没有给出任何结果:

<html>
<head> 
<script type="text/javascript">
function show() {

    alert("hi");

 var json = '{
   "http://x.x.x.x.compute-1.amazonaws.com/": {
      "comments": {
         "data": [
            {
               "id": "f434343",
               "from": {
                  "name": "x",
                  "id": "1666353"
               },
               "message": "testing",
               "can_remove": false,
               "created_time": "2013-10-02T10:47:30+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "181596_319910",
               "from": {
                  "name": "y",
                  "id": "10546353"
               },
               "message": "hi",
               "can_remove": false,
               "created_time": "2013-10-02T07:30:00+0000",
               "like_count": 0,
               "user_likes": false
            }
         ],
         "paging": {
            "cursors": {
               "after": "MQ==",
               "before": "Mg=="
            }
         }
      }
   }
}';

var obj = json_decode($json);
    alert(obj->{'message'});


}
</script>

</head>
<body onload="show()">
<button type="submit" value="click " onclick="show()"> button </button>
</body>
</html>
4

2 回答 2

1

这可以为你做到。(对 foreach 循环中的级别进行编号)

   foreach ($obj as $one) {
        foreach ($one as $two) {
            foreach($two as $three) {
                foreach($three as $four) {
                    print $four->message . '<br />';
                }
            }                
        }
    }
于 2013-10-02T17:02:21.933 回答
1

您用 javascript 编写 php,但不包含 php 标签

function show() {

    alert("hi");
<?php
 $json = '{
   "http://x.x.x.x.compute-1.amazonaws.com/": {
      "comments": {
         "data": [
            {
               "id": "f434343",
               "from": {
                  "name": "x",
                  "id": "1666353"
               },
               "message": "testing",
               "can_remove": false,
               "created_time": "2013-10-02T10:47:30+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "181596_319910",
               "from": {
                  "name": "y",
                  "id": "10546353"
               },
               "message": "hi",
               "can_remove": false,
               "created_time": "2013-10-02T07:30:00+0000",
               "like_count": 0,
               "user_likes": false
            }
         ],
         "paging": {
            "cursors": {
               "after": "MQ==",
               "before": "Mg=="
            }
         }
      }
   }
}';

$obj = json_decode($json);
echo "    alert(" . $obj->{'message'} . ");";

?>

}

此外,您的 JSON 对象不包含顶级消息,消息是嵌套的,因此您需要使用:

$data = $obj->{'http://x.x.x.x.compute-1.amazonaws.com/'}->comments->data;
foreach ($data as $obj) {
    echo "    alert(" . $obj->message . ");";
}
于 2013-10-02T17:02:46.270 回答