在Perl中,检查json解码的布尔值 [英] In Perl, checking a json decoded boolean value

查看:121
本文介绍了在Perl中,检查json解码的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解码的JSON布尔值是对象:

Decoded JSON booleans are objects:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;
use JSON;

my $json_string = '{"boolean_field":true}';
my $decoded_json = from_json $json_string;

print Dumper $decoded_json;

输出:

$VAR1 = {
          'boolean_field' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' )
        };

JSON.pm文档中,我了解以下三种方法:

From the JSON.pm documentation I know about the following three methods:

  • JSON :: is_bool
  • JSON :: true
  • JSON :: false

但是,出于某些愚蠢的原因,我不知道如何确定$decoded_json中的'boolean_field'的值是真还是假.

However, for some silly reason I don't know how to determine if the value of 'boolean_field' in $decoded_json is true or false.

(很抱歉,这是一个非常基本的问题;它一直让我发疯!)

(Sorry for the very basic question; it's been driving me batty!)

推荐答案

在Perl中这将是一个真实的值.只需正常访问即可.

It will be a truthy value in Perl. Just access it as normal.

print 'true' if $decoded_json->{'boolean_field'};

这篇关于在Perl中,检查json解码的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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