JSON到PHP使用数组的file

JSON到PHP使用数组的file

本文介绍了JSON到PHP使用数组的file_get_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取使用API​​的杂志下面的JSON内容。
在JSON的输出是这样的。我想下面的JSON转换为PHP数组。

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"
            }
        ]
    },

]
}

我的code看起来是这样的。

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>";
?>

以上code返回一个空数组。 :(
这怎么可能上面的JSON转换为PHP对象数组。
我很无奈。

The above code returns an empty array. :(How is it possible to convert the above JSON to php object array.I am helpless.

谢谢
阿里汉

推荐答案

您提供的样本JSON是无效的。与此JSON验证在线检查。您需要删除线59额外的逗号。

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.

一,你有你可以使用这个code将其转换为一个数组有效的JSON。

One you have valid json you can use this code to convert it to an array.

json_de code($ 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:49