我正在尝试解析php中的json文件。但我在foreach
收到错误。这是我的PHP代码。
{
"movies":
[
{
"title": "Aarilirunthu Arubathu Varai",
"year": "1979",
"genre": "Drama",
"starting": "Rajinikanth",
},
{
"title": "Vinnaithandi varuvaya",
"year": "2010",
"genre": "Love",
"starting": "Simbu",
},
{
"title": "The Pursuit of happyness",
"year": "2006",
"genre": "Biography",
"starting": "Willsmith",
},
{
"title": "Marly and Me",
"year": "2008",
"genre": "Comedy and drama",
"starting": "Owen wilson",
},
{
"title": "Hachi: A Dog's Tale",
"year": "2009",
"genre": "Loyalty",
"starting": "Hachi",
},
{
"title": "Maalai pozhudin mayakathilay",
"year": "2013",
"genre": "Love",
"starting": "Aari",
},
]
}
<?php
$jsondata = file_get_contents("movies.json");
$json = json_decode($jsondata,true);
$output ="<ul>";
foreach($json ['movies'] as $movies)
{
$output .= "<h4>".$movies['title']."</h4>";
$output .="<li>Year: ".$movies['year']."</li>";
$output .="<li>Genre: ".$movies['genre']."</li>";
$output .="<li>Starting: ".$movies['starting']."</li>";
}
$output .="</ul>";
echo $output ;
?>
有人可以帮我找出错误。提前谢谢。
答案 0 :(得分:3)
删除所有不需要的逗号,JSON中不允许使用它们,
{
"movies":
[
{
"title": "Aarilirunthu Arubathu Varai",
"year": "1979",
"genre": "Drama",
"starting": "Rajinikanth"
},
{
"title": "Vinnaithandi varuvaya",
"year": "2010",
"genre": "Love",
"starting": "Simbu"
},
{
"title": "The Pursuit of happyness",
"year": "2006",
"genre": "Biography",
"starting": "Willsmith"
},
{
"title": "Marly and Me",
"year": "2008",
"genre": "Comedy and drama",
"starting": "Owen wilson"
},
{
"title": "Hachi: A Dog's Tale",
"year": "2009",
"genre": "Loyalty",
"starting": "Hachi"
},
{
"title": "Maalai pozhudin mayakathilay",
"year": "2013",
"genre": "Love",
"starting": "Aari"
}
]
}