简体   繁体   中英

Problems When Grabbing Information in JSON File (PHP)

So I am trying to just grab some data from a json file in php which should be simple but is not working

$url = 'http://www.vam.ac.uk/api/json/museumobject';
$contents = array(file_get_contents($url));
var_dump($contents);

this returns the contents of the file.

Whenever I try to grab data it doesn't work.

 var_dump($contents[0].records.[0].object);

this returns the whole file.

Any suggestions to return just one piece of information in this json file: http://www.vam.ac.uk/api/json/museumobject

Try JSON decoding :

$contents = json_decode(file_get_contents($url));  

// var_dump($contents);

print_r ($contents->records[0]);
print_r ($contents->records[0]->fields);

Use json_decode and then pass true to have it return an associative array if that's needed. Source

$contents = json_decode(file_get_contents($url), true);  
$contents[0]->name

Then your $contents will be accessible as an array and name is an attribute of your object you would like to access.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM