使用 file_get_contents 将 JSON 转换为 PHP 数组 [英] JSON to PHP Array using file_get_contents
问题描述
我正在尝试使用杂志 API 获取以下 json 内容.json的输出是这样的.我希望下面的 json 转换为 php 数组.
<代码>{"bpath": "http://www.sampledomain.com/",列表":[{"cid": "11","display_type": "网格","ctitle": "abc","acount": "71",一个列表": [{"援助": "6865","日期": "2 小时前","atitle": "测试","adesc": "测试描述","aimg": "","aurl": "?nid=6865","weburl": "news.php?nid=6865",cmtcount":0"},{"援助": "6857","日期": "20 小时前","atitle": "test1","adesc": "测试 desc1","aimg": "","aurl": "?nid=6857","weburl": "news.php?nid=6857",cmtcount":0"}]},{"cid": "1","display_type": "网格","ctitle": "test1","账户": "2354",一个列表": [{"援助": "6851","日期": "1 天前","atitle": "test123","adesc": "test123 desc","aimg": "","aurl": "?nid=6851","weburl": "news.php?nid=6851",cmtcount":7"},{"援助": "6847","日期": "2 天前","atitle": "test12345","adesc": "test12345 desc","aimg": "","aurl": "?nid=6847","weburl": "news.php?nid=6847",cmtcount":7"}]},]}
我的代码看起来像这样.
";打印_r($数据);echo "</pre>";?>
上面的代码返回一个空数组.:(如何将上述 JSON 转换为 php 对象数组.我很无奈.
谢谢韩
您提供的 JSON 示例无效.使用此 JSON 验证器 http://jsonlint.com/ 在线检查.您需要删除第 59 行多余的逗号.
如果您拥有有效的 json,您可以使用此代码将其转换为数组.
json_decode($json, true);
数组([bpath] =>http://www.sampledomain.com/[列表] =>大批([0] =>大批([cid] =>11[显示类型] =>网格[标题] =>美国广播公司[帐户] =>71[列表] =>大批([0] =>大批([援助] =>6865[日期] =>2小时前[标题] =>测试[adesc] =>测试说明[aimg] =>[aurl] =>?nid=6865[网址] =>news.php?nid=6865[cmtcount] =>0)[1] =>大批([援助] =>6857[日期] =>20 小时前[标题] =>测试1[adesc] =>测试描述1[aimg] =>[aurl] =>?nid=6857[网址] =>news.php?nid=6857[cmtcount] =>0)))[1] =>大批([cid] =>1[显示类型] =>网格[标题] =>测试1[帐户] =>2354[列表] =>大批([0] =>大批([援助] =>6851[日期] =>1 天前[标题] =>测试123[adesc] =>test123 描述[aimg] =>[aurl] =>?nid=6851[网址] =>news.php?nid=6851[cmtcount] =>7)[1] =>大批([援助] =>6847[日期] =>2天前[标题] =>测试12345[adesc] =>test12345 描述[aimg] =>[aurl] =>?nid=6847[网址] =>news.php?nid=6847[cmtcount] =>7)))))
I am trying to fetch the below json content using a magazine api. The output of the json is like this. i want the below json to convert to php array.
{
"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"
}
]
},
]
}
My code looks like this.
<?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>";
?>
The above code returns an empty array. :( How is it possible to convert the above JSON to php object array. I am helpless.
Thanks Haan
The JSON sample you provided is not valid. Check it online with this JSON Validator http://jsonlint.com/. You need to remove the extra comma on line 59.
One you have valid json you can use this code to convert it to an array.
json_decode($json, true);
Array
(
[bpath] => http://www.sampledomain.com/
[clist] => Array
(
[0] => Array
(
[cid] => 11
[display_type] => grid
[ctitle] => abc
[acount] => 71
[alist] => Array
(
[0] => Array
(
[aid] => 6865
[adate] => 2 Hours ago
[atitle] => test
[adesc] => test desc
[aimg] =>
[aurl] => ?nid=6865
[weburl] => news.php?nid=6865
[cmtcount] => 0
)
[1] => Array
(
[aid] => 6857
[adate] => 20 Hours ago
[atitle] => test1
[adesc] => test desc1
[aimg] =>
[aurl] => ?nid=6857
[weburl] => news.php?nid=6857
[cmtcount] => 0
)
)
)
[1] => Array
(
[cid] => 1
[display_type] => grid
[ctitle] => test1
[acount] => 2354
[alist] => Array
(
[0] => Array
(
[aid] => 6851
[adate] => 1 Days ago
[atitle] => test123
[adesc] => test123 desc
[aimg] =>
[aurl] => ?nid=6851
[weburl] => news.php?nid=6851
[cmtcount] => 7
)
[1] => Array
(
[aid] => 6847
[adate] => 2 Days ago
[atitle] => test12345
[adesc] => test12345 desc
[aimg] =>
[aurl] => ?nid=6847
[weburl] => news.php?nid=6847
[cmtcount] => 7
)
)
)
)
)
这篇关于使用 file_get_contents 将 JSON 转换为 PHP 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!