为什么在couchdb中创建文档时会导致错误的utf8字符错误?

时间:2012-11-22 21:25:44

标签: erlang couchdb

在couchdb中创建文档会产生以下错误,

12> ADoc.
[{<<"Adress">>,<<"Hjalmar Brantingsgatan 7 C">>},
 {<<"District">>,<<"Brämaregården">>},
 {<<"Rent">>,3964},
 {<<"Rooms">>,2},
 {<<"Area">>,0}]
13> IDoc.
[{<<"Adress">>,<<"Segeparksgatan 2A">>},
 {<<"District">>,<<"Kirseberg">>},
 {<<"Rent">>,9701},
 {<<"Rooms">>,3},
 {<<"Area">>,83}]
14> erlang_couchdb:create_document({"127.0.0.1", 5984}, "proto_v1", IDoc).
{json,{struct,[{<<"ok">>,true},
           {<<"id">>,<<"c6d96b5f923f50bfb9263638d4167b1e">>},
           {<<"rev">>,<<"1-0d17a3416d50129328f632fd5cfa1d90">>}]}}
15> erlang_couchdb:create_document({"127.0.0.1", 5984}, "proto_v1", ADoc).
** exception exit: {ucs,{bad_utf8_character_code}}
     in function  xmerl_ucs:from_utf8/1 (xmerl_ucs.erl, line 185)
     in call from mochijson2:json_encode_string/2 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 200)
 in call from mochijson2:'-json_encode_proplist/2-fun-0-'/3 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 181)
 in call from lists:foldl/3 (lists.erl, line 1197)
 in call from mochijson2:json_encode_proplist/2 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 184)
 in call from erlang_couchdb:create_document/3 (/Users/admin/AlphaGroup/src/erlang_couchdb.erl, line 256)

在两个文档之上,可以在couchdb中创建一个没有问题(IDoc)。

任何人都可以帮我弄清楚它的原因吗?

2 个答案:

答案 0 :(得分:2)

我认为问题出现在&lt;&lt;“Brämaregården”&gt;&gt;中。首先必须将unicode转换为二进制。示例在以下链接中。

unicode discussion。核心功能在unicode

答案 1 :(得分:0)

在Erlang代码中输入非ASCII字符非常繁琐,因为它在shell中的工作方式与编译的Erlang代码不同。

尝试显式输入二进制文件为UTF-8:

<<"Br", 16#c3, 16#a4, "mareg", 16#c3, 16#a5, "rden">>

即,“ä”由UTF-8中的字节C3 A4和C3 A5的“å”表示。有很多方法可以找到这些代码;快速搜索this table

通常情况下,您会从代码之外的某处获得输入,例如:从文件中读取,输入网页表格等,然后你就不会遇到这个问题。