-2

我想显示这个json 文件中的以下值。 标题 datum_start datum_end

使用 以下代码:

<?php
$nl1jsonurl='http://www.tvgids.nl/json/lists/programs.php?channels=1';
$nl1jsontext=file_get_contents($nl1jsonurl,true);

$nl1 = json_decode($nl1jsontext);
foreach($nl1->1 as $data)
{
     echo $data->titel . "\n";
     echo $data->datum_start . "\n";
     echo $data->datum_end . "\n";

} 
?>

示例 JSON:

{
    "1": [
        {
            "db_id": "14612024",
            "titel": "NOS Journaal",
            "genre": "Nieuws/actualiteiten",
            "soort": "",
            "kijkwijzer": "",
            "artikel_id": null,
            "artikel_titel": null,
            "datum_start": "2013-07-05 00:10:00",
            "datum_end": "2013-07-05 00:35:00"
        },
        {
            "db_id": "14642893",
            "titel": "NOS Studio Sportzomer: De Avondetappe",
            "genre": "Sport",
            "soort": "",
            "kijkwijzer": "",
            "artikel_id": "58162",
            "artikel_titel": "Sprinten in Montpellier",
            "datum_start": "2013-07-05 00:35:00",
            "datum_end": "2013-07-05 01:30:00"
        }
    ]
}
4

2 回答 2

1

因为您的 JSON 有一个名为 的属性1,所以您必须像这样访问它:

foreach($nl1->{'1'} as $data)

如果您尝试使用 PHP 将产生语法错误$nl1->1

于 2013-07-05T13:53:49.993 回答
-2

这可能会帮助你

<?php 
$nl1jsonurl='http://www.tvgids.nl/json/lists/programs.php?channels=1';
$nl1jsontext=file_get_contents($nl1jsonurl,true);

//decode the content lke this
$nl1 = json_decode($nl1jsontext, TRUE);

//print_r($nl1);

foreach($nl1['1'] as $data)
{
     echo $data['title'] . "\n";
     echo $data['datum_start'] . "\n";
     echo $data['datum_end'] . "\n";

} 
于 2013-07-05T14:13:07.987 回答