In Perl, checking a json decoded boolean value
Decoded JSON booleans are objects:
#!/usr/bin/env perl
开发者_如何学Pythonuse 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;
Output:
$VAR1 = {
'boolean_field' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' )
};
From the JSON.pm documentation I know about the following three methods:
- JSON::is_bool
- JSON::true
- JSON::false
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!)
It will be a truthy value in Perl. Just access it as normal.
print 'true' if $decoded_json->{'boolean_field'};
精彩评论