我的数据库知道utf8
:
List of databases
Name | Owner | Encoding | Collate | Ctype |
-----------+---------------+----------+------------+------------+
tucha | tucha_cleaner | UTF8 | en_US.utf8 | en_US.utf8 |
当我连接到它时,我设置了client_encoding
:
my $hm_schema = App::Schema->connect( $dsn, $user, $pass, {
AutoCommit => 1,
RaiseError => 1,
client_encoding => 'UTF8',
}
);
据我所见,返回值是UTF8:
DBG>$value
["Нд", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"]
DBG>use Devel::Peek
DBG>Devel::Peek::Dump $value
SV = PVMG(0xfe41c20) at 0xfe079b0
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK,UTF8)
IV = 0
NV = 0
PV = 0xfe27550 "[\"\320\235\320\264\", \"\320\237\320\275\", \"\320\222\321\202\", \"\320\241\321\200\", \"\320\247\321\202\", \"\320\237\321\202\", \"\320\241\320\261\"]"\0 [UTF8 "["\x{41d}\x{434}", "\x{41f}\x{43d}", "\x{412}\x{442}", "\x{421}\x{440}", "\x{427}\x{442}", "\x{41f}\x{442}", "\x{421}\x{431}"]"]
CUR = 56
LEN = 58
COW_REFCNT = 4
undef
但是当我尝试通过decode_json
的{{1}}对该字符串进行解码时,我得到了错误:
Mojo::JSON
为什么会出现该错误以及如何解决?
答案 0 :(得分:0)
字符串的前5个字符如下(十六进制):
5B 22 41D 434 22
诸如UTF-8的字符编码是使用字节表示代码点的手段,而其中两个字符不是字节,因此您的字符串不可能使用UTF-8进行JSON编码。
看来您有一个解码的字符串。字符编码已被删除,以产生一个Unicode Code Points字符串。如果是这样,请替换
JSON::decode_json($json_utf8)
JSON::MaybeXS::decode_json($json_utf8)
JSON::PP::decode_json($json_utf8)
JSON::XS::decode_json($json_utf8)
Cpanel::JSON::XS::decode_json($json_utf8)
使用
JSON->new->decode($json_ucp) -or- JSON::from_json($json_ucp)
JSON::MaybeXS->new->decode($json_ucp)
JSON::PP->new->decode($json_ucp)
JSON::XS->new->decode($json_ucp)
Cpanel::JSON::XS->new->decode($json_ucp)
顺便说一句,除非您想了解Perl的内部知识,否则Devel :: Peek不是适合此工作的合适工具。您应该改用Data :: Dumper或类似的工具。
use Data::Dumper qw( Dumper );
# This is the same string as in the OP.
my $value = qq{["\x{41d}\x{434}", "\x{41f}\x{43d}", "\x{412}\x{442}", "\x{421}\x{440}", "\x{427}\x{442}", "\x{41f}\x{442}", "\x{421}\x{431}"]};
local $Data::Dumper::Useqq = 1;
print(Dumper($value));
输出:
$VAR1 = "[\"\x{41d}\x{434}\", \"\x{41f}\x{43d}\", \"\x{412}\x{442}\", \"\x{421}\x{440}\", \"\x{427}\x{442}\", \"\x{41f}\x{442}\", \"\x{421}\x{431}\"]";