以前关于 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>