首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Perl解析JSON

用Perl解析JSON
EN

Stack Overflow用户
提问于 2017-02-13 16:07:25
回答 1查看 2.2K关注 0票数 1

我试图在Perl中解析JSON数据。这是对思科的主要服务的要求。我的脚本工作,但解析不起作用。我有个警告,

代码语言:javascript
运行
复制
         malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "HTTP::Response=HASH(...") at InternetBSP.pl line 39.

它就在这里:

代码语言:javascript
运行
复制
      my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($res);

没有艾迪我该怎么修理它..。

代码语言:javascript
运行
复制
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";
}

我也有这样的结果:

代码语言:javascript
运行
复制
   {"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" 

但它应该是这样的:

代码语言:javascript
运行
复制
     {"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,

更新后:)

代码语言:javascript
运行
复制
   ------------------------
   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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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";}

我会重写这整块代码。

代码语言:javascript
运行
复制
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的方法调用,而不是间接的对象表示法。

最后,我们是decodeing $res->content

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42208476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档