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 $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 ->
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With