开发者

What is the difference of these JSON commands?

开发者 https://www.devze.com 2023-03-15 09:32 出处:网络
What is the difference of $json->encode() and to_json() ? use JSON; my $json = JSON->new->allow_nonref;

What is the difference of $json->encode() and to_json() ?

use JSON;
my $json = JSON->new->allow_nonref;
$json = $json->utf8;

$data->{$id} = $json->encode(\%{$act->{$id}});
$json_string = to_json($data);

from perldoc JSON

$json_text = encode_json $perl_scalar
Converts the given Perl data structure to a UTF-8 encoded, binary string.

$json_text = to_json($perl_scalar)
Converts the given Perl data structure to a json string.

$json_text = $json->encode($perl_scalar)
Converts the given Perl data structure (a simple scalar or a reference to a hash or array)
to its JSON representation. Simple scalars will be converted into JSON string or number
sequences, while references to arrays become JSON arrays and references to hashes become
JSON objects. Undefined Perl values (e.g. "u开发者_运维技巧ndef") become JSON "null" values.  References
to the integers 0 and 1 are converted into "true" and "false".

What's the difference, and when do I know which to use?


It's just a functional vs. OO interface to your JSON features. OO interface allows you to configure the JSON object before using it.

See FUNCTIONAL INTERFACE in JSON.

0

精彩评论

暂无评论...
验证码 换一张
取 消