There are boolean values in the JSON data structure I am using. When call decode_json
to convert it to a Perl data structure and feed to the XMLout
function provided by XML::Simple
, it throws an error because XMLout
does not know how to deal with JSON::XS::Boolean
values.
我正在使用的JSON数据结构中有布尔值。当调用decode_json将其转换为Perl数据结构并提供给XML :: Simple提供的XMLout函数时,它会抛出一个错误,因为XMLout不知道如何处理JSON :: XS :: Boolean值。
Is there a way to convert the JSON::XS::Boolean
values in a data structure to XML?
有没有办法将数据结构中的JSON :: XS :: Boolean值转换为XML?
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
my $rec = XMLout( $result, RootName => 'root', SuppressEmpty => 1);
In the code abive, I get the following error - Can't encode a value of type: JSON::XS::Boolean
在代码abive中,我得到以下错误 - 无法编码类型的值:JSON :: XS :: Boolean
A print Dumper $result
gives:
打印Dumper $结果给出:
$result = {
'a' => 'x',
'b' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' )
};
2 个解决方案
#1
I asked the same question on PerlMonks and am reproducing the proposed solution below.
我在PerlMonks上问了同样的问题,并且正在复制下面提出的解决方案。
Basically, the solution is to change the value of JSON::XS::Boolean to an appropriate value before passing it to XMLout:
基本上,解决方案是在将JSON :: XS :: Boolean传递给XMLout之前将其更改为适当的值:
use strict;
use warnings;
use JSON;
use XML::Simple;
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
for my $value ( values %$result ) {
next unless 'JSON::XS::Boolean' eq ref $value;
$value = ( $value ? 'true' : 'false' );
}
print XMLout( $result, RootName => 'root', SuppressEmpty => 1);
Output:
C:\Temp> test.pl
<root a="x" b="true" />
#2
Edit: I wrote this answer before all the edits to the original question. The question as stated now is that the original poster wants to create an XML-ready structure for using with XML::Simple; originally stated, it seemed that he just wanted to put the JSON structure in a text node.
编辑:我在对原始问题的所有编辑之前写了这个答案。现在提出的问题是,原始海报想要创建一个XML就绪结构,以便与XML :: Simple一起使用;最初说,似乎他只想将JSON结构放在文本节点中。
Perl objects need to be JSON-encoded before sending them through the wire.
Perl对象在通过线路发送之前需要进行JSON编码。
From your example:
从你的例子:
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
print JSON->new->utf8->pretty(1)->encode($result);
You get the following:
你得到以下:
$ perl json.pl
{
"a" : "x",
"b" : true
}
#1
I asked the same question on PerlMonks and am reproducing the proposed solution below.
我在PerlMonks上问了同样的问题,并且正在复制下面提出的解决方案。
Basically, the solution is to change the value of JSON::XS::Boolean to an appropriate value before passing it to XMLout:
基本上,解决方案是在将JSON :: XS :: Boolean传递给XMLout之前将其更改为适当的值:
use strict;
use warnings;
use JSON;
use XML::Simple;
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
for my $value ( values %$result ) {
next unless 'JSON::XS::Boolean' eq ref $value;
$value = ( $value ? 'true' : 'false' );
}
print XMLout( $result, RootName => 'root', SuppressEmpty => 1);
Output:
C:\Temp> test.pl
<root a="x" b="true" />
#2
Edit: I wrote this answer before all the edits to the original question. The question as stated now is that the original poster wants to create an XML-ready structure for using with XML::Simple; originally stated, it seemed that he just wanted to put the JSON structure in a text node.
编辑:我在对原始问题的所有编辑之前写了这个答案。现在提出的问题是,原始海报想要创建一个XML就绪结构,以便与XML :: Simple一起使用;最初说,似乎他只想将JSON结构放在文本节点中。
Perl objects need to be JSON-encoded before sending them through the wire.
Perl对象在通过线路发送之前需要进行JSON编码。
From your example:
从你的例子:
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
print JSON->new->utf8->pretty(1)->encode($result);
You get the following:
你得到以下:
$ perl json.pl
{
"a" : "x",
"b" : true
}