in reply to Converting entities in JSON context
binmode *STDOUT, ':encoding(UTF-8)';
Perl JSON modules keep the strings in parsed structures as characters, but when serializing to JSON strings, they use bytes and UTF-8 encoding.
Update:
According to the specification, JSON doesn't use entities, but it can use the \uXXXX notation, so instead of using HTML::Entities, you can try
sub convert { my ($s) = @_; $s =~ s/&#x([[:xdigit:]]{4});/\\u$1/gr } my $entities_json = '{"school":"Eötvös Loránd Uni +versity"}'; my $converted_json = convert($entities_json); print "Original JSON: [$entities_json]\n"; print "Converted JSON: [$converted_json]\n"; # [{"school":"E\u00F6tv\ +u00F6s Lor\u00E1nd University"}] my $decoded_json = decode_json($converted_json); binmode STDOUT, ':encoding(UTF-8)'; print "School: " . $decoded_json->{'school'} . "\n"; # Eötvös Loránd +University
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting entities in JSON context
by Anonymous Monk on May 19, 2022 at 12:58 UTC | |
by choroba (Cardinal) on May 19, 2022 at 13:00 UTC | |
by Anonymous Monk on May 19, 2022 at 13:11 UTC |