我似乎无法正确使用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, not encode_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“用法"发牢骚
发布评论