简体   繁体   English

Perl LWP以字符串形式返回JSON输出

[英]Perl LWP returns JSON output as string

I am using Perl LWP::UserAgent to get response from an API. 我正在使用Perl LWP :: UserAgent从API获得响应。 Everything works good except one issue. 除了一个问题,一切都正常。

The API that i am using it returns response in JSON format. 我正在使用的API以JSON格式返回响应。 But I am getting it as string when i get the response through LWP module, Something like below. 但是当我通过LWP模块获得响应时,它以字符串形式获取,如下所示。

$VAR1 = '
{"status":"success","data":[{"empid":"345232","customername":"Lee gates","dynamicid":"2342342332sd32423"},{"empid":"36.VLXP.013727..CBCL..","customername":"Lee subdirectories","dynamicid":"223f3423dsf23423423"}],"message":""}'

I did " print Dumper $response " to get the output. 我做了“ print Dumper $response ”来获取输出。

One more thing, The challenge is that my client do not want to go with Perl module for JSON (use JSON::Parse 'parse_json';). 另一件事是,挑战是我的客户不想使用Perl模块获取JSON(使用JSON :: Parse'parse_json';)。

Any help would be appreciated! 任何帮助,将不胜感激!

You need to decode the JSON string into a Perl data structure. 您需要将JSON字符串解码为Perl数据结构。 If your version of perl is 5.14+, JSON::PP is included in core, so nothing to install. 如果您的perl版本是5.14+,则JSON::PP包含在核心中,因此无需安装任何内容。

use warnings;
use strict;

use Data::Dumper;
use JSON::PP qw(decode_json);

my $json = '{"status":"success","data":[{"empid":"345232","customername":"Lee gates","dynamicid":"2342342332sd32423"},{"empid":"36.VLXP.013727..CBCL..","customername":"Lee subdirectories","dynamicid":"223f3423dsf23423423"}],"message":""}';

my $perl = decode_json $json;

print Dumper $perl;

Output: 输出:

$VAR1 = {
      'status' => 'success',
      'message' => '',
      'data' => [
                  {
                    'dynamicid' => '2342342332sd32423',
                    'customername' => 'Lee gates',
                    'empid' => '345232'
                  },
                  {
                    'empid' => '36.VLXP.013727..CBCL..',
                    'customername' => 'Lee subdirectories',
                    'dynamicid' => '223f3423dsf23423423'
                  }
                ]
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM