[英]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 因此,我试图从php中的json文件中获取一些数据,该数据应该很简单但无法正常工作
$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 任何建议仅在此json文件中返回一条信息的建议: http : //www.vam.ac.uk/api/json/museumobject
Try JSON decoding : 尝试JSON解码:
$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. 使用
json_decode
,然后传递true,以使其在需要时返回一个关联数组。 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. 然后,您的
$contents
将作为数组访问,并且name
是您要访问的对象的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.