查找json文件中是否存在变量

时间:2016-06-04 15:31:58

标签: php json

我想知道如果json文件中存在变量,我怎么能创建一个条件。有时候我得到一个带有错误属性的文件,有时候没有。所有都取决于我在我的url地址写的内容。所以我需要做出这个条件。 感谢

1 个答案:

答案 0 :(得分:0)

使用file_get_contents()获取JSON文件的内容:

$str = file_get_contents('http://example.com/example.json/');

现在使用json_decode()解码JSON:

$json = json_decode($str, true); // decode the JSON into an associative array

您有一个包含所有信息的关联数组。要查看某些房产,您需要:

if (isset($json['<key>'])){
  // Do something
}
相关问题