Jump to content

JSON Parsing - Noob


icu222much

Recommended Posts

I am trying to extract the list of people in my JSON file, but I keep on receiving an error. I am unsure on how to extract the different people as the names of the people are placed in an array.

 

<?php 

$jsonFile = file_get_contents('list.json'); 
$jsonArray = json_decode($jsonFile, true);

foreach($jsonArray -> people as $p) {
   echo $p;   
}

?>

 

[
  {
    "place": "paris",
    "people": [
      "will", 
      "jake", 
      "melissa"
    ] 
  }, 
  {
    "place": "saigon", 
    "people": [
      "willis",
      "john"
    ]
  }
]

Link to comment
Share on other sites

I have used the var_dump function and I see all of my data from the JSON file being printed.

 

PHP gives me the following errors:

Notice: Trying to get property of non-object in C:\xampp\htdocs\jonTest\test.php on line 6

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\jonTest\test.php on line 6

 

The problem is in the foreach loop. I'm not sure how to tell it to print out each person in the people array.

Link to comment
Share on other sites

I don´t know what is your array like but try with a for loop.

<?php
$jsonFile = file_get_contents('list.json'); 
$jsonArray = json_decode($jsonFile, true);

for($i=0;$i<count($jsonArray);$i++){
print $jsonArray[$i];
}
?>

If it does not work please execute following code and post results here

<?php
$jsonFile = file_get_contents('list.json'); 
$jsonArray = json_decode($jsonFile, true);

print_r($jsonArray);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.