我是PHP的新手,并不是很清楚如何用PHP解析JSON。这是我从第三方获得的JSON
{ "data":
{ "current_condition":
[
{"cloudcover": "0", "humidity": "13", "observation_time": "05:47 AM", "precipMM": "0.0",
"pressure": "1016", "temp_C": "20", "temp_F": "69",
"visibility": "10", "weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
" winddir16Point": "SW", "winddirDegree": "218", "windspeedKmph": "12", "windspeedMiles": "7"
}
],
"request": [
{"query": "Lat 32.12 and Lon 76.53", "type": "LatLon" }
],
"weather": [
{
"date": "2012-11-04", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "69", "tempMinC": "1", "tempMinF": "34",
"weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
"winddir16Point": "SW", "winddirDegree": "218", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "8"
},
{
"date": "2012-11-05", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "67", "tempMinC": "4", "tempMinF": "39",
"weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
"winddir16Point": "SSW", "winddirDegree": "210", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "7"
}
]
}
}
我将此天气信息作为JSON数据获取,其中包含以下信息
我不想要所有信息,只需要特定的信息,如
current_condition
temp_C
temp_F
weatherDesc
我希望接下来2天的天气信息中提供一些数据
我在PHP中尝试了这段代码
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($weather, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
这似乎给了我数组格式的JSON解码数据,但我对如何从PHP数据中获取这些特定值感到困惑。我可以迭代数据
foreach ($jsonIterator as $key => $value) {
if(is_array($value)) {
foreach ($value as $key => $value) {
}
} else {
// echo "$key\n";
}
但不确定如何获取如上所述的值。任何帮助或资源指针都会非常有用
答案 0 :(得分:3)
为什么不使用json_decode
然后处理生成的对象?
示例:http://codepad.org/ciw3ogAu
我使用了ob_get_content()
,因为我不想搞砸转义序列,但重点是这一行:
$result = json_decode($my_json_string);
获取信息并不困难。例如,如果您希望当前温度为摄氏度:
echo $result->data->current_condition[0]->temp_C;
您可以获得未来两天的数组(http://codepad.org/bhOcd3eT):
$weatherarray = $result->data->weather; // An array with two elements
您使用$result->xxx
代替$result["xxx"]
,因为json_decode
将为对象创建对象。您可以通过调用json_decode($my_json_string, true)
将其更改为数组,然后使用以下方式访问成员:
echo $result["data"]["current_condition"][0]["temp_C"];
答案 1 :(得分:2)
您需要解码您的json对象,但不需要迭代它。只需访问您想要的信息:
$decoded = json_decode($weather);
$date = $data->current_condition->data->weather->date;
$tempMaxC = $data->current_condition->data->weather->tempMaxC;
$tempMinC = $data->current_condition->data->weather->tempMinC;
$weatherUrl = $data->current_condition->data->weather->weatherIconUrl;
$windspeedKmph = $data->current_condition->data->weather->windspeedKmph;
答案 2 :(得分:1)
我会采用这种方法来访问您的数据:
$data = json_decode($weather);
然后你可以通过这种方式轻松获得你想要的东西:
print_r($data->data->current_condition);
或循环......
答案 3 :(得分:-1)
$decode=json_decode($file);
echo $decode->data->current_condition[0]->temp_C;
echo $decode->data->current_condition[0]->temp_F;
echo $decode->data->current_condition[0]->weatherDesc[0]->value;
foreach ($decode->data->weather as $data) {
echo $data->date;
echo $data->tempMaxC;
echo $data->tempMinC;
echo $data->weatherIconUrl;
echo $data->windspeedKmph;
}