加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl 处理 json 数据

发布时间:2020-12-15 23:51:28 所属栏目:大数据 来源:网络整理
导读:http://xiaofeng1982.blog.163.com/blog/static/315724582014511102541328/ 安装两个插件 (我的perl版本是window strawberry perl,强烈建议用这个版本,因为在window 下面安装一些perl 包涉及nmake 等编译问题,常常让开发者在window系统下受挫,strawberry
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 => $valuen";
? ? }
#遍历 fuel_stations,由于fuel_stations 是个数组,下面是遍历该数组引用
foreach $a(@{$decoded_json->{fuel_stations}} )
? #此数组每个元素又是hash结构,继续遍历为每个数组元素遍历 hash 的各元素,与前面遍历hash类似
? print $a->{access_days_time},$a->{distance},u5b8bu4f53; font-size:16px"> ? foreach $key(keys %{$a})
? {
? ? print $key,u5b8bu4f53; font-size:16px"> ? } ?
? last;
#附上 dump json 出来的结果的数据结构
$VAR1 = {
? ? ? ? ? 'longitude' => '-104.9847179',
? ? ? ? ? 'station_locator_url' => 'http://www.afdc.energy.gov/afdc/locator/stations/',u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'total_results' => 40,u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'latitude' => '39.737567',u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'fuel_stations' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'access_days_time' => 'Garage business hours; pay lot',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'distance' => '0.2571',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'station_phone' => '303-640-1096',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'state' => 'CO',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_connector_types' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'J1772',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'CHADEMO'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?],u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'hy_status_link' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'latitude' => '39.735271',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'open_date' => '2012-12-01',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'id' => 50065,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'plus4' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'longitude' => '-104.9885199',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'fuel_type_code' => 'ELEC',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_network' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'zip' => '80204',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'updated_at' => '2014-04-21T18:28:50Z',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'e85_blender_pump' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'intersection_directions' => 'Located on level 3 by the elevator',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'groups_with_access_code' => 'Public',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ng_vehicle_class' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'cards_accepted' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_level2_evse_num' => 2,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'bd_blends' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_network_web' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'lpg_primary' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ng_psi' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'city' => 'Denver',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'street_address' => '65 W 12th Ave',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ng_fill_type_code' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'expected_date' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'status_code' => 'E',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_dc_fast_num' => 1,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_level1_evse_num' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_other_evse' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'owner_type_code' => 'LG',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'station_name' => 'Cultural Center Complex Garage',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'date_last_confirmed' => '2014-04-21',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'geocode_status' => '200-8'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'access_days_time' => '24 hours daily',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'distance' => '0.30189',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'station_phone' => '888-758-4389',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'J1772'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'latitude' => '39.7377044',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'open_date' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'id' => 53942,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'longitude' => '-104.9903829',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_network' => 'ChargePoint Network',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'updated_at' => '2014-06-11T08:08:05Z',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_network_ids' => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'posts' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '1:74433'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'intersection_directions' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_level2_evse_num' => 1,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_network_web' => 'http://www.mychargepoint.net/',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'street_address' => '1345 Bannock St',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'ev_dc_fast_num' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'owner_type_code' => undef,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'station_name' => 'NATL CAR CHRGNG',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'date_last_confirmed' => '2014-06-11',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'geocode_status' => 'GPS'
],u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'precision' => {
? ? ? ? ? ? ? ? ? ? ? ? ? ?'types' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'locality',u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'political'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ],u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ?'value' => 4,u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ?'name' => 'locality'
? ? ? ? ? ? ? ? ? ? ? ? ?},u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'station_counts' => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'fuels' => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'LNG' => {
? ? ? ?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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'E85' => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'total' => 1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},u5b8bu4f53; font-size:16px"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'total' => 59
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },u5b8bu4f53; font-size:16px"> ? ? ? ? ? 'offset' => 0
? ? ? ? };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读