如何在linux机器上引用perl中的本地JSON文件?
如何引用本地存储的json文件?我正试图用perl访问它。例如,如何从这个json文件访问经度和纬度 代码如下:如何在linux机器上引用perl中的本地JSON文件?,json,perl,Json,Perl,如何引用本地存储的json文件?我正试图用perl访问它。例如,如何从这个json文件访问经度和纬度 代码如下: #!/usr/bin/perl use JSON qw(decode_json); $address = '/address_verification.json'; my $json=decode_json($address); print $json 以下是JSON: [ { "input_index": 0, "candid
#!/usr/bin/perl
use JSON qw(decode_json);
$address = '/address_verification.json';
my $json=decode_json($address);
print $json
以下是JSON:
[
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "XXX XX XXXth Rd",
"last_line": "Utica IL XXXXX-XXXX",
"delivery_point_barcode": "456529758",
"components": {
"primary_number": "XXXX",
"street_predirection": "X",
"street_name": "XXXth",
"street_suffix": "Rd",
"city_name": "XXXX",
"default_city_name": "XXXX",
"state_abbreviation": "XX",
"zipcode": "XXXXXX",
"plus4_code": "XXXX",
"delivery_point": "XX",
"delivery_point_check_digit": "X"
},
"metadata": {
"record_type": "X",
"zip_type": "Standard",
"county_fips": "XXXXXX",
"county_name": "XXXXXXX",
"carrier_route": "XXXXX",
"congressional_district": "XX",
"rdi": "Residential",
"elot_sequence": "XXXX",
"elot_sort": "X",
"latitude": 55.555,
"longitude": -55.5555,
"precision": "XXXX",
"time_zone": "XXXXX",
"utc_offset": -5,
"dst": true
},
"analysis": {
"dpv_match_code": "X",
"dpv_footnotes": "XXXXX",
"dpv_cmra": "X",
"dpv_vacant": "X",
"active": "X",
"footnotes": "X#"
}
}
]
我不断返回以下错误:
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "/address_verificatio...") at ./validateaddress.ajax line 8.
目录大致按以下方式构建:
HTML
-->CSS
-->Modules
---->Weather
------>address_verification.json
------>file_calling_json.file
您的文件名
/address\u verification.json
似乎使用了不正确的路径。这是指在根目录中找到的文件。文件路径必须是绝对路径,或者是相对于运行Perl程序的目录的有效路径
例如,如果您从示例中的目录HTML
运行程序,您可以将该文件称为Modules/Weather/address\u-verification.json
除此之外,您需要先将json文件读入Perl变量,然后才能对其进行解码。下面是如何完成此操作的示例:
use strict;
use warnings;
use Data::Dumper;
use JSON;
my $address = 'address_verification.json';
my $txt = do { # do block to read file into variable
local $/; # slurp entire file
open my $fh, "<", $address or die $!; # open for reading
<$fh>; # read and return file content
};
my $json = decode_json($txt);
print Dumper $json
/address\u verification.json
引用根目录中的文件。你说你的文件在HTML/CSS/Modules/Weather
中。您需要使用绝对地址或有效的相对地址引用该文件。此外,您可能需要正确读取文件。请参阅文档。另外,decode_json
需要json,而不是文件系统路径。您需要打开文件,读取它,并将其内容传递给decode\u json
,而不仅仅是传递文件名。@ikegami这是如何正确完成的?我最初指定了路径,但遇到了相同的问题。文档的真实路径是/var/www/html/modules/weather,但我一直返回格式错误的json错误。“格式错误的json”错误是因为decode\u json()
希望传递一个包含json的字符串。它不需要文件名。打开文件并从文件中读取JSON是您的责任。然后,您可以将该JSON字符串传递到decode_JSON()
,并返回一个Perl数据结构。谢谢,这非常有效。现在要遍历散列,我必须导航键。对于我的$i($json){print$i->{“components”};}……这看起来像是……对吗?不,你有一个数组[
,它包含一个散列{
,它包含了其他值的混合。你可能想对我的$hash(@$json){对我的$key(keys%$hash){print$hash->{$key}做
以下程序正在尝试访问元数据数组。我是否调用了不正确的键?对于我的$hash(@$json){对于我的$key(sort keys%$hash){对于我的$metadata(sort keys%{$hash{$key}{'metadata'}}}{print$metadata,“\n”;}}}@senicad如果您对如何使用Perl数据结构有疑问,请查看Perl文档,或者提出一个新问题
$VAR1 = [
{
'components' => {
'primary_number' => 'XXXX',
'delivery_point' => 'XX',
'plus4_code' => 'XXXX',
'street_predirection' => 'X',
'street_name' => 'XXXth',
'zipcode' => 'XXXXXX',
'street_suffix' => 'Rd',
'default_city_name' => 'XXXX',
'state_abbreviation' => 'XX',
'delivery_point_check_digit' => 'X',
'city_name' => 'XXXX'
},
'delivery_point_barcode' => '456529758',
'last_line' => 'Utica IL XXXXX-XXXX',
'analysis' => {
'dpv_footnotes' => 'XXXXX',
'dpv_vacant' => 'X',
'dpv_match_code' => 'X',
'active' => 'X',
'dpv_cmra' => 'X',
'footnotes' => 'X#'
},
'candidate_index' => 0,
'metadata' => {
'elot_sort' => 'X',
'elot_sequence' => 'XXXX',
'county_fips' => 'XXXXXX',
'longitude' => '-55.5555',
'congressional_district' => 'XX',
'rdi' => 'Residential',
'carrier_route' => 'XXXXX',
'zip_type' => 'Standard',
'record_type' => 'X',
'utc_offset' => -5,
'latitude' => '55.555',
'time_zone' => 'XXXXX',
'county_name' => 'XXXXXXX',
'dst' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' ),
'precision' => 'XXXX'
},
'input_index' => 0,
'delivery_line_1' => 'XXX XX XXXth Rd'
}
];