I am trying to parse json file in php. But am getting error in foreach
. This is my php code.
{
"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 ;
?>
Can someone help me to find out the error. Thanks in advance.
Remove all unwanted commas , they are not allowed in 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"
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.