从Perl中的JSON-String解码哈希

前端之家收集整理的这篇文章主要介绍了从Perl中的JSON-String解码哈希前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么这不起作用?

@H_502_8@

@H_502_8@

my $myHashEncoded = encode_json \%myHash;
my %myHashDecoded = decode_json($myHashEncoded);

我收到错误:@H_502_8@

@H_502_8@

Reference found where even-sized list expected at ...

所以我改成了:@H_502_8@

@H_502_8@

my $myHashEncoded = encode_json \%myHash;
my $myHashDecoded = decode_json($enableInputEncoded);

但显然%myHash与$myHashDecoded不同.@H_502_8@

如何从JSON字符串恢复正确的哈希?@H_502_8@

解决方法

假设您使用的是JSON.pm,the documentation says

@H_502_8@

@H_502_8@

The opposite of encode_json: expects an UTF-8 (binary) string and tries to parse that as an UTF-8 encoded JSON text,returning the resulting reference.@H_502_8@

所以你要收回你所放入的内容.你正在放入一个hashref,然后你就会得到一个hashref.@H_502_8@

如果你想要一个常规哈希,那么你只需取消引用它就像你对其他hashref一样:@H_502_8@

@H_502_8@

my $myHashRefDecoded = decode_json($myHashEncoded);
my %myHashDecoded = %$myHashRefDecoded;

猜你在找的Perl相关文章