通过perl解析以JSON编码的数组 [英] Parsing an array encoded in JSON through perl

查看:95
本文介绍了通过perl解析以JSON编码的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下Perl代码通过 JSON模块来解析JSON中的数组.但是返回的数组长度为1,我无法对其进行正确的迭代.所以问题是我无法使用返回的数组.

I am using the Following Perl code to parse an array in JSON, using the JSON module. But the array returned has length 1 and I am not able to iterate over it properly. So the problem is I am not able to use the array returned.

#!/usr/bin/perl
use strict;

my $json_text = '[ {"name" : "abc", "text" : "text1"}, {"name" : "xyz", "text" : "text2"} ]';

use JSON;
use Data::Dumper::Names;

my @decoded_json = decode_json($json_text);
print Dumper(@decoded_json), length(@decoded_json), "\n";

输出为:

$VAR1 = [
     {
        'text' => 'text1',
        'name' => 'abc'
      },
      {
        'text' => 'text2',
        'name' => 'xyz'
      }
    ];
1

推荐答案

The decode_json function returns an arrayref, not a list. You must dereference it to get the list:

my @decoded_json = @{decode_json($json_text)};

您可能想阅读 perldoc perlreftut

You may want to read perldoc perlreftut and perldoc perlref

这篇关于通过perl解析以JSON编码的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆