В чем отличие этих команд JSON? - PullRequest
3 голосов
/ 23 июня 2011

В чем разница $json->encode() и to_json()?

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

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

от 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. "undef") become JSON "null" values.  References
to the integers 0 and 1 are converted into "true" and "false".

В чем разница, и когда я знаю, какой использовать?

1 Ответ

3 голосов
/ 23 июня 2011

Это просто функциональный интерфейс против OO для ваших функций JSON. OO-интерфейс позволяет настроить объект JSON перед его использованием.

См. ФУНКЦИОНАЛЬНЫЙ ИНТЕРФЕЙС in JSON.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...