开发者

Need example of Perl JSON usage

how do I parse a JSON string in Perl?

Note: I am a C++/Java/C# software engineer, not a Perl-wielding scripter.

I have fully read the JSON.pm "documentation" to find a working example, but those docs turned out to be complete and utter garbage".

I also read this post, but no one seems to know how to get the JSON key, value pairs out of the decoded perl variable.

Here is my script. Can someone help?

use JSON;

my 开发者_JAVA技巧$json_string = '{"foo": "bar"}';
my $decoded_json = decode_json($json_text);
print $decoded_json["foo"];

std::map<string, string> mymap = convert_to_cplusplus(decoded_json); # doesn't work
my asfjsa;fjsa;fwe # doesn't work

I got frustrated near the end.


You need to use $decoded_json->{"foo"}---note the curly brackets, not the square ones. Also note the arrow ->---decode_json returns a reference.

I will readily admit that the documentation in this case is no help for someone who doesn't already know Perl, for example, to the level of knowing what $perl_hash_or_arrayref means. (A seasoned Perl programmer knows instantly it means you have to use ->.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜