php和json阅读字符串

时间:2012-12-01 21:34:24

标签: php json

使用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 ) )

但关键是我只能读取整数但不能读取字符串为什么:

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) (...) } }

P.S我有一些粘贴和格式化所有print_r结果的问题所以我不得不用'(..)'

来讨论一下

由于

0 个答案:

没有答案