【发布时间】:2012-03-20 17:01:19
【问题描述】:
我正在尝试使用杂志 api 获取以下 json 内容。 json的输出是这样的。我希望下面的 json 转换为 php 数组。
{
"bpath": "http://www.sampledomain.com/",
"clist": [
{
"cid": "11",
"display_type": "grid",
"ctitle": "abc",
"acount": "71",
"alist": [
{
"aid": "6865",
"adate": "2 Hours ago",
"atitle": "test",
"adesc": "test desc",
"aimg": "",
"aurl": "?nid=6865",
"weburl": "news.php?nid=6865",
"cmtcount": "0"
},
{
"aid": "6857",
"adate": "20 Hours ago",
"atitle": "test1",
"adesc": "test desc1",
"aimg": "",
"aurl": "?nid=6857",
"weburl": "news.php?nid=6857",
"cmtcount": "0"
}
]
},
{
"cid": "1",
"display_type": "grid",
"ctitle": "test1",
"acount": "2354",
"alist": [
{
"aid": "6851",
"adate": "1 Days ago",
"atitle": "test123",
"adesc": "test123 desc",
"aimg": "",
"aurl": "?nid=6851",
"weburl": "news.php?nid=6851",
"cmtcount": "7"
},
{
"aid": "6847",
"adate": "2 Days ago",
"atitle": "test12345",
"adesc": "test12345 desc",
"aimg": "",
"aurl": "?nid=6847",
"weburl": "news.php?nid=6847",
"cmtcount": "7"
}
]
},
]
}
我的代码如下所示。
<?php
$json_url = "http://api.testmagazine.com/test.php?type=menu";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
上面的代码返回一个空数组。 :( 如何将上述 JSON 转换为 php 对象数组。 我很无奈。
谢谢 韩
【问题讨论】:
-
您可能会得到一个空数组,因为 JSON 对象格式不正确。尝试通过JSONLint 验证它。
-
print_r($data) 的输出是什么?
-
@Jack & Aldo - 解决了我的问题。 Shivam S.Kara 代码,终于成功了。
标签: php arrays file-get-contents json