Perl 什么是检查对象是否在 JSON 内定义、丢失或为空的最佳方法
Perl what is the best way to check if an object defined, missing or null within JSON
我下面有一个 JSON 文件,我想检查 3 个状态
在数组“categories”中,我有另一个数组“children”,它当前为空
我怎样才能知道
- 如果子数组为空?
- 如果子数组已定义且包含至少一个数据?
- 如果 JSON 中完全缺少子数组,而我期待在这里
在JSON文件下方
{
"id": "Store::REZZ",
"name": "Rezz",
"categories": [
{
"id": "Category::0556",
"name": "Cinéma",
"children": []
},
{
"id": "Category::0557",
"name": "Séries",
"children": []
}
],
"images": [
{
"format": "logo",
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1920px-Google_2015_logo.svg.png",
"withTitle": false
}
],
"type": "PLAY"
}
我尝试了一些,但我只能处理案例 1。对于其他案例,我收到“不是哈希引用”的错误消息
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON qw( decode_json );
use JSON qw( from_json );
# JSON file
my $json_f = '/home/test';
# JSON text
my $json_text = do {
open (TOP, "<", $json_f);
local $/;
<TOP>
};
my $data = from_json($json_text);
my @tags = @{ $data->{"categories"}{"children"} };
if (@tags) {
foreach (@tags) {
say $_->{"name"};
say "1. array is ok and contains data";
}
} elsif (@tags == 0) {
say "3. array is empty";
} else {
say "2. array is missing";
}
__END__
1.if children array is null ?
if (!defined($data->{categories})) { ... }
- if children array is defined and contain at least one data ?
if (defined($data->{categories}) && @{$data->{categories}} ) { ... }
- if children array is completely missing from the JSON whereas I was expecting to be here
if (!exists $data->{categories}) { ... }
Data::Dumper
将让您可视化 JSON 转换成的 perl 数据结构。在你的例子中,
$VAR1 = {
'images' => [
{
'withTitle' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1920px-Google_2015_logo.svg.png',
'format' => 'logo'
}
],
'id' => 'Store::REZZ',
'name' => 'Rezz',
'categories' => [
{
'children' => [],
'id' => 'Category::0556',
'name' => "Cin\x{e9}ma"
},
{
'id' => 'Category::0557',
'name' => "S\x{e9}ries",
'children' => []
}
],
'type' => 'PLAY'
};
从这里可以看出,$data->{"categories"}
是 hashrefs 的 arrayref,而不是 hashref 本身。
您可以迭代其元素:
foreach my $cat (@{$data->{categories}}) {
if (!exists $cat->{children}) {
# No children element
} elsif (@{$cat->{children}} == 0) {
# Empty array
} else {
# Has at least element in the array
}
}
我下面有一个 JSON 文件,我想检查 3 个状态
在数组“categories”中,我有另一个数组“children”,它当前为空
我怎样才能知道
- 如果子数组为空?
- 如果子数组已定义且包含至少一个数据?
- 如果 JSON 中完全缺少子数组,而我期待在这里
在JSON文件下方
{
"id": "Store::REZZ",
"name": "Rezz",
"categories": [
{
"id": "Category::0556",
"name": "Cinéma",
"children": []
},
{
"id": "Category::0557",
"name": "Séries",
"children": []
}
],
"images": [
{
"format": "logo",
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1920px-Google_2015_logo.svg.png",
"withTitle": false
}
],
"type": "PLAY"
}
我尝试了一些,但我只能处理案例 1。对于其他案例,我收到“不是哈希引用”的错误消息
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON qw( decode_json );
use JSON qw( from_json );
# JSON file
my $json_f = '/home/test';
# JSON text
my $json_text = do {
open (TOP, "<", $json_f);
local $/;
<TOP>
};
my $data = from_json($json_text);
my @tags = @{ $data->{"categories"}{"children"} };
if (@tags) {
foreach (@tags) {
say $_->{"name"};
say "1. array is ok and contains data";
}
} elsif (@tags == 0) {
say "3. array is empty";
} else {
say "2. array is missing";
}
__END__
1.if children array is null ?
if (!defined($data->{categories})) { ... }
- if children array is defined and contain at least one data ?
if (defined($data->{categories}) && @{$data->{categories}} ) { ... }
- if children array is completely missing from the JSON whereas I was expecting to be here
if (!exists $data->{categories}) { ... }
Data::Dumper
将让您可视化 JSON 转换成的 perl 数据结构。在你的例子中,
$VAR1 = {
'images' => [
{
'withTitle' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1920px-Google_2015_logo.svg.png',
'format' => 'logo'
}
],
'id' => 'Store::REZZ',
'name' => 'Rezz',
'categories' => [
{
'children' => [],
'id' => 'Category::0556',
'name' => "Cin\x{e9}ma"
},
{
'id' => 'Category::0557',
'name' => "S\x{e9}ries",
'children' => []
}
],
'type' => 'PLAY'
};
从这里可以看出,$data->{"categories"}
是 hashrefs 的 arrayref,而不是 hashref 本身。
您可以迭代其元素:
foreach my $cat (@{$data->{categories}}) {
if (!exists $cat->{children}) {
# No children element
} elsif (@{$cat->{children}} == 0) {
# Empty array
} else {
# Has at least element in the array
}
}