【问题标题】:Perl LWP::UserAgent parse response JSONPerl LWP::UserAgent 解析响应 JSON
【发布时间】:2021-12-01 15:03:34
【问题描述】:

我正在使用LWP::UserAgent 模块向我们的其中一个 API 发出 GET 请求。

#!/usr/bin/perl
use strict;
use warning;
use LWP::UserAgent;
use Data::Dumper;

my $ua = LWP::UserAgent->new;
my $request = $ua->get("http://example.com/foo", Authorization => "Bearer abc123", Accept => "application/json" );

print Dumper $request->content;

请求成功。 Dumper 返回以下 JSON。

$VAR1 = '{
          "apiVersion": "v1",
          "data": {
              "ca-bundle.crt": "-----BEGIN CERTIFICATE-----abc123-----END CERTIFICATE-----\\n"
          },
          "kind": "ConfigMap",
          "metadata": {
              "creationTimestamp": "2021-07-16T17:13:01Z",
              "labels": {
                  "auth.openshift.io/managed-certificate-type": "ca-bundle"
              },
              "managedFields": [
                  {
                      "apiVersion": "v1",
                      "fieldsType": "FieldsV1",
                      "fieldsV1": {
                          "f:data": {
                              ".": {},
                              "f:ca-bundle.crt": {}
                          },
                          "f:metadata": {
                              "f:labels": {
                                  ".": {},
                                  "f:auth.openshift.io/managed-certificate-type": {}
                              }
                          }
                      },
                      "manager": "cluster-kube-apiserver-operator",
                      "operation": "Update",
                      "time": "2021-09-14T17:07:39Z"
                  }
              ],
              "name": "kube-control-plane-signer-ca",
              "namespace": "openshift-kube-apiserver-operator",
              "resourceVersion": "65461225",
              "selfLink": "/api/v1/namespaces/openshift-kube-apiserver-operator/configmaps/kube-control-plane-signer-ca",
              "uid": "f9aea067-1234-5678-9101-9d4073f5ae53"
          }
      }';

假设我想打印 apiVersion 键的值,它应该打印 v1

print "API Version = $request->content->{'apiVersion'} \n";

正在打印以下内容。我不确定如何打印值 v1。由于 HTTP::Response 包含在输出中,我怀疑我可能必须使用 HTTP::Response module?

API Version = HTTP::Response=HASH(0x2dffe80)->content->{'apiVersion'}

【问题讨论】:

  • 您应该首先使用例如将 JSON 字符串输出解析为 Perl 哈希。 JSON::XS。然后您可以在该哈希上使用语法:$hash->{apiVersion}

标签: json perl curl httpresponse lwp-useragent


【解决方案1】:

Perl 不会在双引号字符串中展开子例程调用。

print "API Version = $request->content->{'apiVersion'} \n";

在这行代码中,content() 是一个子程序调用。因此 Perl 将其视为:

print "API Version = $request" . "->content->{'apiVersion'} \n";

如果您尝试打印大多数 Perl 对象,您将获得哈希引用以及类的名称 - 因此是 HTTP::Response=HASH(0x2dffe80)

你可能认为你只需要像这样分解你的 print() 语句:

print 'API Version = ', $request->content->{'apiVersion'}, "\n";

但这也行不通。 $request->content 不返回 Perl 数据结构,它返回 JSON 编码的字符串。您需要先将其解码为数据结构,然后才能访问各个元素。

use JSON;

print 'API Version = ', decode_json($request->content)->{'apiVersion'}, "\n";

但在print() 语句之外进行解码可能更简洁。

use JSON;

my $data = decode_json($request->content);

在这种情况下,您可以返回到更像原始代码的内容:

print "API Version = $data->{'apiVersion'} \n";

【讨论】:

    【解决方案2】:

    必须先解码 JSON 内容。为此有几个模块,例如JSON:

    use JSON;
    # ...
    my $href = decode_json $request->content;
    

    然后像普通的哈希引用一样使用它:$href->{apiVersion}

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 2023-03-04
      • 2016-06-16
      • 2020-01-11
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多