我在使用来自http://www.wunderground.com/ API 的pvp 读取 json 响应时遇到问题。我发现读取除整数以外的任何内容都有问题。我在网上寻找,但我的问题没有合适的答案。最后我放弃了...
编码:
$json_string = file_get_contents($w);
$json_string = utf8_encode($json_string);
$result = json_decode($json_string,true);
结果print_r($result);
给了我完整的正确答案
Array ( [response] => Array ( [version] (...) Europe/Warsaw [local_tz_offset] => +0100 [weather] => Scattered Clouds [temperature_string] => 30 F (-1 C) [temp_f] => 30 [temp_c] => -1 [relative_humidity] => 93% [wind_string] => From the SE at 8 MPH [wind_dir] => SE [wind_degrees] => 130 [wind_mph] => 8 [wind_gust_mph] => 0 [wind_kph] => 13 [wind_gust_kph] => 0 [pressure_mb] => 1013 [pressure_in] => 29.92 [pressure_trend] => 0 [dewpoint_string] => 28 F (-2 C) [dewpoint_f] => 28 [dewpoint_c] => -2 [heat_index_string] => NA [heat_index_f] => NA [heat_index_c] => NA [windchill_string] => 23 F (-5 C) [windchill_f] => 23 [windchill_c] => -5 [feelslike_string] => 23 F (-5 C) [feelslike_f] => 23 [feelslike_c] => -5 [visibility_mi] => 6.2 [visibility_km] => 10.0 [solarradiation] => [UV] => 1 [precip_1hr_string] => -9999.00 in (-9999.00 mm) [precip_1hr_in] => -9999.00 [precip_1hr_metric] => -9999.00 [precip_today_string] => 0.00 in (0.0 mm) [precip_today_in] => 0.00 [precip_today_metric] => 0.0 [icon] => partlycloudy [icon_url] => (..) [forecast_url] =>(..) [history_url] => (..) [ob_url] =>(..)52.16999817,20.96999931 ) )
但关键是我只能读取 Integer 而不是 string 不知道为什么:
echo "Current temperature in ${location} is: ${temp_c}\n";
echo "Current temperature F in ${location} is: ${temp_f}\n";
echo "Current humidity F in ${location} is: ${hum}\n";
echo "Current wind in ${location} is: ${wind}\n";
输出如下所示:
Current temperature in Warsaw is: -3
Current temperature F in Warsaw is: 27
Current humidity F in Warsaw is:
Current wind in Warsaw is:
因此,正如我之前提到的,除了整数之外,我无法从 json 中读取任何内容。使用 php 版本:
php -v
PHP 5.3.6 (cli) (built: Apr 14 2011 14:58:35)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
花了几个小时寻找解决方案后仍然找不到我的问题的答案。如果您知道任何线索,请告诉我,因为现在我一直被这个问题困扰着,我想永远。
var_dump($result)
也给了我适当的数组:
array(3) { ["response"] (...)["weather"]=> string(16) "Scattered Clouds" ["temperature_string"]=> string(11) "30 F (-1 C)" ["temp_f"]=> int(30) ["temp_c"]=> int(-1) ["relative_humidity"]=> string(3) "93%" ["wind_string"]=> string(20) "From the SE at 8 MPH" ["wind_dir"]=> string(2) "SE" ["wind_degrees"]=> int(130) ["wind_mph"]=> int(8) ["wind_gust_mph"]=> int(0) ["wind_kph"]=> int(13) ["wind_gust_kph"]=> int(0) ["pressure_mb"]=> string(4) "1013" ["pressure_in"]=> string(5) "29.92" ["pressure_trend"]=> string(1) "0" ["dewpoint_string"]=> string(11) "28 F (-2 C)" ["dewpoint_f"]=> int(28) ["dewpoint_c"]=> int(-2) ["heat_index_string"]=> string(2) "NA" ["heat_index_f"]=> string(2) "NA" ["heat_index_c"]=> string(2) "NA" ["windchill_string"]=> string(11) "23 F (-5 C)" ["windchill_f"]=> string(2) "23" ["windchill_c"]=> string(2) "-5" ["feelslike_string"]=> string(11) "23 F (-5 C)" ["feelslike_f"]=> string(2) "23" ["feelslike_c"]=> string(2) "-5" ["visibility_mi"]=> string(3) "6.2" ["visibility_km"]=> string(4) "10.0" ["solarradiation"]=> string(0) "" ["UV"]=> string(1) "1" ["precip_1hr_string"]=> string(25) "-9999.00 in (-9999.00 mm)" ["precip_1hr_in"]=> string(8) "-9999.00" ["precip_1hr_metric"]=> string(8) "-9999.00" ["precip_today_string"]=> string(16) "0.00 in (0.0 mm)" ["precip_today_in"]=> string(4) "0.00" ["precip_today_metric"]=> string(3) "0.0" ["icon"]=> string(12) "partlycloudy" ["icon_url"]=> string(47) "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif" ["forecast_url"]=> string(54) (..) ["history_url"]=> string(76) (..) ["ob_url"]=> string(89) (...) } }
PS我在粘贴和格式化所有print_r结果时遇到了一些问题,所以我不得不用'(..)'来解决一些问题
谢谢