魏长东

weichangdong

东邪

perl解析json

[root@swordman perl]# cat  json_t
{"root_path":"pictures\/bbm","count":"15704340","country":"ID","data_type":"1"}
{"root_path":"dcim\/facebook","count":"10421769","country":"ID","data_type":"1"}
{"root_path":"dcim\/100andro","count":"8437408","country":"ID","data_type":"1"}
{"root_path":"android\/data\/com.qihoo.security","count":"8370765","country":"ID","data_type":"1"}
{"root_path":"backucup","count":"7734925","country":"ID","data_type":"1"}
{"root_path":"pictures\/b612","count":"7432353","country":"ID","data_type":"1"}
{"root_path":"pictures\/instagram","count":"6518579","country":"ID","data_type":"1"}
{"root_path":"backucup","count":"6046648","country":"IN","data_type":"1"}
{"root_path":"dcim\/facebook","count":"5875220","country":"IN","data_type":"1"}
{"root_path":"android\/data\/com.qihoo.security","count":"5705046","country":"IN","data_type":"1"}
{"root_path":"android\/data\/com.qihoo.security","count":"4948008","country":"US","data_type":"1"}
{"root_path":"dcim\/100andro","count":"4767237","country":"IN","data_type":"1"}

具体代码

 

use strict;
use JSON;
use Encode;
use Data::Dumper;
open(WCD,'<json_t') or  die('error'.$!);
#my $json = new JSON;
my $json = JSON->new->utf8;
while(<WCD>){
	#print Dumper($_);
	my $ok = decode_json($_);
	#my $ok = $json->decode($_);
	print $ok->{'country'} ." => ".$ok->{'data_type'}." => ".$ok->{'count'}." => ".$ok->{'root_path'};
	#my $qq = Dumper($ok);print  $qq;
	print "\n";
}

执行结果
[root@swordman perl]# perl  test_js.pl 
ID => 1 => 15704340 => pictures/bbm
ID => 1 => 10421769 => dcim/facebook
ID => 1 => 8437408 => dcim/100andro
ID => 1 => 8370765 => android/data/com.qihoo.security
ID => 1 => 7734925 => backucup
ID => 1 => 7432353 => pictures/b612
ID => 1 => 6518579 => pictures/instagram
IN => 1 => 6046648 => backucup
IN => 1 => 5875220 => dcim/facebook
IN => 1 => 5705046 => android/data/com.qihoo.security
US => 1 => 4948008 => android/data/com.qihoo.security
IN => 1 => 4767237 => dcim/100andro