http://xiaofeng1982.blog.163.com/blog/static/315724582014511102541328/
安装两个插件 (我的perl版本是window strawberry perl, 强烈建议用这个版本,因为在window 下面安装一些perl 包涉及nmake 等编译问题,常常让开发者在window系统下受挫,strawberry perl 里面已经集成了这些乱七八糟的东西。所以安装perl 包非常方便)
##ppm install JSON-XS
##ppm install JSON
另外你还可以用cpan 来装perl 包 (dos 下)
如
cpan
install perl 包名 (包名直接从cpan 网站copy过来就行)
use LWP::Simple;
use Data::Dumper;
#网络web service API,通过LWP::Simple get 方法获取json 格式内容 $content
$url='https://api.data.gov/nrel/alt-fuel-stations/v1/nearest.json?api_key=UXAQVylZSvlT1D1h9Aipj96oOzselNWkCHnZi7fU&location=Denver+CO';
$content=get($url);
print $content;
#或者从文件读取json格式内容 $content
open $file,"out1";
read($file, $content, -s "out1");
close $file;
use JSON;
my $decoded_json = decode_json( $content );
print Dumper $decoded_json; #打印json数据结构和数据
my $json_text = $decoded_json->{longitude}; #获取各个item的值 ,如 longitude
print $json_text;
#遍历第一层的key 和值
foreach $key(keys %{$decoded_json})
{
#print $key,'
',$decoded_json->{$key}, "\n";
}
##另外一种遍历hash方法,仅供参考
while (($key, $value) = each %{$decoded_json}) {
print "$k =>\n";
while (($key2, $value) = each %$value) {
print "\t$key2 => $value\n";
}
}
#遍历 fuel_stations,由于fuel_stations 是个数组,下面是遍历该数组引用
foreach $a(@{$decoded_json->{fuel_stations}} )
{
#此数组每个元素又是hash结构,继续遍历为每个数组元素遍历 hash 的各元素,与前面遍历hash类似
print $a->{access_days_time},'
',$a->{distance},"\n";
foreach $key(keys %{$a})
{
print $key,"\n";
}
last;
}
#附上 dump json 出来的结果的数据结构
$VAR1 = {
'longitude' => '-104.9847179',
'station_locator_url' => 'http://www.afdc.energy.gov/afdc/locator/stations/',
'total_results' => 40,
'latitude' => '39.737567',
'fuel_stations' => [
{
'access_days_time' => 'Garage business hours; pay lot',
'distance' => '0.2571',
'station_phone' => '303-640-1096',
'state' => 'CO',
'ev_connector_types' => [
'J1772',
'CHADEMO'
],
'hy_status_link' => undef,
'latitude' => '39.735271',
'open_date' => '2012-12-01',
'id' => 50065,
'plus4' => undef,
'longitude' => '-104.9885199',
'fuel_type_code' => 'ELEC',
'ev_network' => undef,
'zip' => '80204',
'updated_at' => '2014-04-21T18:28:50Z',
'e85_blender_pump' => undef,
'intersection_directions' => 'Located on level 3 by the elevator',
'groups_with_access_code' => 'Public',
'ng_vehicle_class' => undef,
'cards_accepted' => undef,
'ev_level2_evse_num' => 2,
'bd_blends' => undef,
'ev_network_web' => undef,
'lpg_primary' => undef,
'ng_psi' => undef,
'city' => 'Denver',
'street_address' => '65 W 12th Ave',
'ng_fill_type_code' => undef,
'expected_date' => undef,
'status_code' => 'E',
'ev_dc_fast_num' => 1,
'ev_level1_evse_num' => undef,
'ev_other_evse' => undef,
'owner_type_code' => 'LG',
'station_name' => 'Cultural Center Complex Garage',
'date_last_confirmed' => '2014-04-21',
'geocode_status' => '200-8'
},
{
'access_days_time' => '24 hours daily',
'distance' => '0.30189',
'station_phone' => '888-758-4389',
'state' => 'CO',
'ev_connector_types' => [
'J1772'
],
'hy_status_link' => undef,
'latitude' => '39.7377044',
'open_date' => undef,
'id' => 53942,
'plus4' => undef,
'longitude' => '-104.9903829',
'fuel_type_code' => 'ELEC',
'ev_network' => 'ChargePoint Network',
'zip' => '80204',
'updated_at' => '2014-06-11T08:08:05Z',
'ev_network_ids' => {
'posts' => [
'1:74433'
]
},
'e85_blender_pump' => undef,
'intersection_directions' => undef,
'groups_with_access_code' => 'Public',
'ng_vehicle_class' => undef,
'cards_accepted' => undef,
'ev_level2_evse_num' => 1,
'bd_blends' => undef,
'ev_network_web' => 'http://www.mychargepoint.net/',
'lpg_primary' => undef,
'ng_psi' => undef,
'city' => 'Denver',
'street_address' => '1345 Bannock St',
'ng_fill_type_code' => undef,
'expected_date' => undef,
'status_code' => 'E',
'ev_dc_fast_num' => undef,
'ev_level1_evse_num' => undef,
'ev_other_evse' => undef,
'owner_type_code' => undef,
'station_name' => 'NATL CAR CHRGNG',
'date_last_confirmed' => '2014-06-11',
'geocode_status' => 'GPS'
},
],
'precision' => {
'types' => [
'locality',
'political'
],
'value' => 4,
'name' => 'locality'
},
'station_counts' => {
'fuels' => {
'LNG' => {
'total' => 0
},
'ELEC' => {
'stations' => {
'total' => 27
},
'total' => 46
},
'LPG' => {
'total' => 6
},
'HY' => {
'total' => 0
},
'CNG' => {
'total' => 5
},
'BD' => {
'total' => 1
},
'E85' => {
'total' => 1
}
},
'total' => 59
},
'offset' => 0
};