在Perl中解码Unicode JSON的问题 [英] Problem with decoding unicode JSON in perl

查看:158
本文介绍了在Perl中解码Unicode JSON的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试解码来自PHP脚本的 json_encode 功能.我将问题简化为下一个代码:

I experience a strange behavior in Perl while trying to decode a Unicode JSON string coming from a PHP script's json_encode function. I simplified the problem to next code:

#!/usr/bin/perl
use CGI;
use JSON;
print CGI::header(-type=>'text/html', -charset=>'UTF-8');

print %{ decode_json('{"test_1" : "= \u00F9 ="}') }->{'test_1'};
print '<br>';
print %{ decode_json('{"test_2" : "= \u00F9 \u0121 ="}') }->{'test_2'};

当我在浏览器中运行此脚本时,我会看到下一个:

When I run this script in browser I see next:

= � =
= ù ġ =

第一行包含一个断字符",第二行是正确的.我认为这是由于某种原因,Perl会以ISO-8859-1编码解码第一个字符串,如果我将页面编码更改为ISO-8859-1,则第一行是正确的,但是第二行是坏的.

The first line contains a "broken character", the second is correct. What I think is happenning is that for some reason Perl decodes first string in ISO-8859-1 encoding, if I change page encoding to ISO-8859-1 the first line is correct, however the second is broken.

我的Perl版本是5.10.1,JSON版本是2.51.

My Perl version is 5.10.1 and the JSON version is 2.51.

问题:如何强制Perl json_decode在第一次打印时返回UTF-8字符?

Question: how to force Perl json_decode to return UTF-8 characters in the first print?

注意:我可以通过将第一个输出手动转换为UTF-8来解决此问题,但这需要安装一个额外的编码器"模块,我想避免这种情况.

Note: I can fix the problem by manually converting first output to UTF-8, but this requires the installation of an additional "Encoder" module, which I want to avoid.

推荐答案

对代码进行了尝试,并生成了带有使用警告;"的警告.

Tried your code and it generated several warnings with "use warnings;"

如果您想确保获得utf8,我相信您必须告诉Perl.使用"binmode(STDOUT,":utf8);"或类似的内容.

If you want to be sure to get utf8 I believe you have to tell Perl so. Use "binmode(STDOUT, ":utf8");" or similar.

这在命令行上有效:

use strict;
use warnings;
use JSON;

binmode(STDOUT, ":utf8");

print decode_json('{"test_1" : "= \u00F9 ="}')->{test_1};
print '<br>';
print decode_json('{"test_2" : "= \u00F9 \u0121 ="}')->{'test_2'};

AFAIK,这不会影响decode_json(),但会影响perl脚本本身的输出. Unicode教程通常会告诉您在输入&输出(文件处理程序)

AFAIK, this does not affect decode_json(), but the output from the perl script itself. Unicode tutorials often tell you to explicitly state what encoding you want on your input & output (filehandlers)

这篇关于在Perl中解码Unicode JSON的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆