问题描述
我似乎无法正确使用JSON::XS
的OO接口.以下错误伴我无法追踪:
I can't seem to use JSON::XS
's OO interface properly. The following croaks with an error I can't track down:
use JSON::XS;
my $array = ['foo', 'bar'];
my $coder = JSON::XS->new->utf8->pretty;
print $coder->encode_json($array);
此错误与以下内容有关:Usage: JSON::XS::encode_json(scalar) at test.pl line 5.
我一直在梳理JSON::XS
的代码,但在任何地方都找不到用法:"警告.我的用法似乎与文档中的示例非常匹配.谁能告诉我我哪里出问题了?
This croaks with the following: Usage: JSON::XS::encode_json(scalar) at test.pl line 5.
I have been combing through the code for JSON::XS
and I can't find a "Usage:" warning anywhere. My usage seems to be pretty well matched with the examples in the documentation. Can anyone tell me where I have gone wrong?
推荐答案
JSON::XS
具有两个接口:功能和面向对象.
JSON::XS
has two interfaces: functional and OO.
- 在功能界面中,功能名称为
encode_json
. - 在OO界面中,方法只是
encode
,而不是encode_json
.
- In the functional interface, the function name is
encode_json
. - In the OO interface, the method is simply
encode
, notencode_json
.
以下两个摘录均起作用:
Both of the following two snippets work:
# Functional | # OO
------------------------------+-----------------------------------------
|
use JSON::XS; | use JSON::XS;
my $array = ['foo', 'bar']; | my $array = [ 'foo', 'bar' ];
|
print encode_json($array); | my $coder = JSON::XS->new->utf8->pretty;
| print $coder->encode($array);
|
# ["foo","bar"] | # [
| # "foo",
| # "bar"
| # ]
这篇关于JSON :: XS“用法"发牢骚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!