I'm trying to get data from the following JSON file using PHP. I specifically want "temperatureMin" and "temperatureMax".
我正在尝试使用PHP从以下JSON文件中获取数据。我特别想要"temperatureMin"和"temperatureMax"。
It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("file.json"). Some help would be greatly appreciated!
这可能真的很简单,但我不知道怎么做。在file_get_contents(“file.json”)之后,我被困在了该做什么。非常感谢您的帮助!
{
"daily": {
"summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
"icon": "clear-day",
"data": [
{
"time": 1383458400,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1383491266,
"sunsetTime": 1383523844,
"temperatureMin": -3.46,
"temperatureMinTime": 1383544800,
"temperatureMax": -1.12,
"temperatureMaxTime": 1383458400,
}
]
}
}
3 个解决方案
#1
195
Get the content of the JSON file using file_get_contents()
:
使用file_get_contents()获取JSON文件的内容:
$str = file_get_contents('http://example.com/example.json/');
Now decode the JSON using json_decode()
:
现在使用json_decode()解码JSON:
$json = json_decode($str, true); // decode the JSON into an associative array
You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:
有一个包含所有信息的关联数组。要了解如何访问所需的值,可以执行以下操作:
echo '<pre>' . print_r($json, true) . '</pre>';
This will print out the contents of the array in a nice readable format. Note that the second parameter is set to true
in order to let print_r()
know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:
这将以一种可读的格式打印出数组的内容。注意,第二个参数被设置为true,以便让print_r()知道应该返回输出(而不是直接打印到屏幕上)。然后,您访问您想要的元素,如下所示:
$temperatureMin = $json['daily']['data'][0]['temperatureMin'];
$temperatureMax = $json['daily']['data'][0]['temperatureMax'];
Or loop through the array however you wish:
或循环遍历数组,但您希望:
foreach ($json['daily']['data'] as $field => $value) {
// Use $field and $value here
}
演示!
#2
8
Use json_decode to transform your JSON into a PHP array. Example:
使用json_decode将您的JSON转换成一个PHP数组。例子:
$json = '{"a":"b"}';
$array = json_decode($json, true);
echo $array['a']; // b
#3
4
Try:
$data = file_get_contents ("file.json");
$json = json_decode($data, true);
foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br/>';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br/>';
}
}
}
#1
195
Get the content of the JSON file using file_get_contents()
:
使用file_get_contents()获取JSON文件的内容:
$str = file_get_contents('http://example.com/example.json/');
Now decode the JSON using json_decode()
:
现在使用json_decode()解码JSON:
$json = json_decode($str, true); // decode the JSON into an associative array
You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:
有一个包含所有信息的关联数组。要了解如何访问所需的值,可以执行以下操作:
echo '<pre>' . print_r($json, true) . '</pre>';
This will print out the contents of the array in a nice readable format. Note that the second parameter is set to true
in order to let print_r()
know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:
这将以一种可读的格式打印出数组的内容。注意,第二个参数被设置为true,以便让print_r()知道应该返回输出(而不是直接打印到屏幕上)。然后,您访问您想要的元素,如下所示:
$temperatureMin = $json['daily']['data'][0]['temperatureMin'];
$temperatureMax = $json['daily']['data'][0]['temperatureMax'];
Or loop through the array however you wish:
或循环遍历数组,但您希望:
foreach ($json['daily']['data'] as $field => $value) {
// Use $field and $value here
}
演示!
#2
8
Use json_decode to transform your JSON into a PHP array. Example:
使用json_decode将您的JSON转换成一个PHP数组。例子:
$json = '{"a":"b"}';
$array = json_decode($json, true);
echo $array['a']; // b
#3
4
Try:
$data = file_get_contents ("file.json");
$json = json_decode($data, true);
foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br/>';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br/>';
}
}
}