JSON 到 PHP 数组使用 file_get_contents

Posted

技术标签:

【中文标题】JSON 到 PHP 数组使用 file_get_contents【英文标题】:JSON to PHP Array using file_get_contents 【发布时间】: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 代码,终于成功了。 【参考方案1】:

检查一些错字','

<?php
 //file_get_content(url);
$jsonD = '
    "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"
                
            ]
        
    ]

';

$parseJ = json_decode($jsonD,true);

print_r($parseJ);
?>

【讨论】:

【参考方案2】:

您的 JSON 不是 P. Galbraith 上面告诉您的有效字符串。

这是解决方案。

<?php 
$json_url = "http://api.testmagazine.com/test.php?type=menu";
$json = file_get_contents($json_url);
$json=str_replace(',

]',"

]",$json);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

使用此代码,它将为您工作。

【讨论】:

我知道您正在从 API 获取数据,因此您无法验证它。但是我给定的代码对你有用 能否请您选择我的答案作为正确答案,以便其他人使用。【参考方案3】:

您提供的 JSON 示例无效。使用此 JSON 验证器 http://jsonlint.com/ 在线检查。您需要删除第 59 行多余的逗号。

如果您拥有有效的 json,您可以使用此代码将其转换为数组。

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
                                )

                        )

                )

        )

)

【讨论】:

以上是关于JSON 到 PHP 数组使用 file_get_contents的主要内容,如果未能解决你的问题,请参考以下文章

Json 对象到 PHP 数组 - 不转换

从 $_POST 中的 json 读取关联数组

PHP数组到JSON字符串

从循环查询 PHP MYSQL JSON 合并数组

PHP 将变量发送到 file_get_contents()

您可以将 PHP $_POST 变量设置为数组吗?