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
https://forums.phpfreaks.com/topic/238998-json-parsing-noob/
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
https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228027
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
https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228032
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.