0

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.

6
  • 1
    And whats the error message? Commented Sep 3, 2014 at 5:23
  • 2
    That JSON has an invalid format :) Remove the extra commas after the last elements of each sub object Commented Sep 3, 2014 at 5:25
  • And after that, here you go : eval.in/187510 :P Commented Sep 3, 2014 at 5:28
  • thank u. i will do it and come back. Commented Sep 3, 2014 at 5:59
  • the error message is, Warning: Invalid argument supplied for foreach() in Commented Sep 3, 2014 at 6:00

1 Answer 1

3

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


      ]
  }
Sign up to request clarification or add additional context in comments.

2 Comments

even though i removed a all the unwanted commas as u said, am getting the same error like, --->Warning: Invalid argument supplied for foreach() in
There is no error on my system, you may want to chack file_get_contents function, make sure it's getting correct file and data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.