mochijson2 decode chinese有错误异常throw:invalid_utf8

时间:2014-08-29 05:24:47

标签: json erlang mochijson2

mochijson2:decode(<<“{{”strKey \“:\”中国\“,\”intKey \“:10,\”arrayKey \“:[1,2,3]}”> >)

 ** exception throw: invalid_utf8
   in function  mochijson2:tokenize_string_fast/2 (src/mochijson2.erl, line 424)
   in call from mochijson2:tokenize_string/2 (src/mochijson2.erl, line 391)
   in call from mochijson2:decode1/2 (src/mochijson2.erl, line 326)
   in call from mochijson2:decode_object/3 (src/mochijson2.erl, line 354)
   in call from mochijson2:json_decode/2 (src/mochijson2.erl, line 321)

1 个答案:

答案 0 :(得分:1)

这不是mochijson2的错。 当您通过Erlang控制台输入utf8二进制文件时,您必须通过在二进制文件末尾添加/utf8来明确告诉shell将其解释为utf8。如果你不这样做,它会尝试以某种奇怪的方式解释它。

1> <<"中国">>.
<<"-ý">> % wrong representation
2> <<"中国"/utf8>>.
<<228,184,173,229,155,189>> % ok representation

只有在使用Erlang shell时才会出现问题。如果你将相同的二进制文件放在没有/utf8的文件中,它就可以正常工作。

所以,在Erlang shell中,试试这个:

1> mochijson2:decode(<<"{\"strKey\":\"中国\", \"intKey\":10, \"arrayKey\":[1, 2, 3]}"/utf8>>).
{struct,[{<<"strKey">>,<<228,184,173,229,155,189>>},
     {<<"intKey">>,10},
     {<<"arrayKey">>,[1,2,3]}]}