我试图在Perl中解析JSON数据。这是对思科的主要服务的要求。我的脚本工作,但解析不起作用。我有个警告,
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "HTTP::Response=HASH(...") at InternetBSP.pl line 39.
它就在这里:
my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($res);
没有艾迪我该怎么修理它..。
use strict;
use warnings;
use JSON -support_by_pp;
use LWP 5.64;
use LWP::UserAgent;
use MIME::Base64;
use REST::Client;
use IO::Socket::SSL;
#So dass es auch ohne SSL Sertifizierung funktioniert
BEGIN { $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0 }
#Create a user agent object
my $ua = LWP::UserAgent->new(
ssl_opts => {
SSL_verify_mode => SSL_VERIFY_NONE(),
verify_hostname => 0,
}
);
#Create a request
my $req = HTTP::Request->new( GET => 'https://10.10.10.10/webacs/api/v1/data/AccessPoints.json?.full=true' );
$req->content_type('application/json');
$req->authorization_basic( "Username", "Password" );
#Pass request to the user agent and get a response back
my $res = $ua->request($req);
#Check the outcome of the Response
if ( $res->is_success ) {
print $res->content;
} else {
print $res->status_line, "n";
}
my $json = new JSON;
my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($res);
#my try to pasre the data
foreach my $ap ( @{ $json_text->{queryResponse}->{'entity'} } ) {
print "------------------------\nAccess Point " . $ap->{'accessPointsDTO'}->{'@id'} . "\n";
print "Model:" . $ap->{'accessPointsDTO'}->{'model'} . "\n";
print "MAC Address:" . $ap->{'accessPointsDTO'}->{'macAddress'} . "\n";
print "Serial Number:" . $ap->{'accessPointsDTO'}->{'serialNumber'} . "\n";
print "Software Version:" . $ap->{'accessPointsDTO'}->{'softwareVersion'} . "\n";
print "Status:" . $ap->{'accessPointsDTO'}->{'status'} . "\n";
print "Location:" . $ap->{'accessPointsDTO'}->{'location'} . "\n";
}
我也有这样的结果:
{"queryResponse":{"@last":"7","@first":"0","@count":"8","@type":"AccessPoints","@responseType":"listEntityInstances","@requestUrl":"https:\/\/10.66.1.23\/webacs\/api\/v1\/ data\/AccessPoints?.full=true","@rootUrl":"https:\/\/10.66.1.23\/webacs\/api\/v1\/data","entity":[{"@dtoType":"accessPointsDTO","@type":"AccessPoints","@url":"https:\/\/10 .66.1.23\/webacs\/api\/v1\/data\/AccessPoints\/205320"
但它应该是这样的:
{"queryResponse":
{"@type":"AccessPoints",
"@rootUrl":"https://172.18.138.90/webacs/api/v1/data",
"@requestUrl":"https://172.18.138.90/webacs/api/v1/data/AccessPoints?.full=true",
"@responseType":"listEntityInstances",
"entity":[
{"@url":"https://172.18.138.90/webacs/api/v1/data/AccessPoints/13544533",
"@type":"AccessPoints",
"@dtoType":"accessPointsDTO",
"accessPointsDTO":
{"@id":"13544533",
"@displayName":"13544533",
"adminStatus":"ENABLE",
"bootVersion":"12.4.23.0",
"clientCount":0,
更新后:)
------------------------
Access Point 205320
Model:AIR-LAP1142N-E-K9
MAC Address:6c:9c:ed:b5:45:60
Serial Number:FCZ1544W51B
Software Version:7.6.130.0
Status:CLEARED
Location:de.bw.stu.
------------------------
Access Point 205322
Model:AIR-CAP3502I-E-K9
MAC Address:0c:f5:a4:ee:70:10
Serial Number:FCZ184680VB
Software Version:7.6.130.0
Status:CLEARED
Location:de.bw.stu.
------------------------
Access Point 205324
Model:AIR-LAP1142N-E-K9
MAC Address:6c:9c:ed:86:9d:20
Serial Number:FCZ1544W50Y
Software Version:7.6.130.0
Status:CLEARED
Location:de.bw.stu.
发布于 2017-02-13 16:13:56
格式错误的JSON字符串,没有数组、对象、数字、字符串或原子,在字符偏移量0处(在"HTTP::Response=HASH(...")之前)
此错误消息意味着您提供给decode
的数据不是JSON。
您正在将$res
传递给decode
,这是一个HTTP::回应对象(参见上文,强调我的)。您需要使用$res->content
,用于调试上面几行的输出。
如果($res->is_success) {打印$res->内容;} {print $res->status_line,"n";}
我会重写这整块代码。
die $res->status_line unless $res->is_success;
my $json = JSON->new->allow_nonref
->utf8->relaxed
->escape_slash->loose
->allow_singlequote->allow_barekey;
my $json_text = $json->decode( $res->content );
而不是打印一些调试输出,然后继续运行,如果出了问题,您只需要die
,如果请求没有成功。
之后,创建JSON对象并对其进行配置。这比这一行代码的可读性强得多,我们使用的是对new
的方法调用,而不是间接的对象表示法。
最后,我们是decode
ing $res->content
。
https://stackoverflow.com/questions/42208476
复制相似问题