json - Erlang - 转换二进制可读UTF

标签 json erlang yaws

我想将 mnesia 数据库中的 YAWS 信息转换为网络。 现在我有代码:

-define(RECORD_TYPE,      albums).
-define(RECORD_KEY_FIELD, album_id).

-record(?RECORD_TYPE,
        {?RECORD_KEY_FIELD, artist_id, album, albumpath, image}).

convert_to_json(Lines) ->
    Data = [{obj,
             [{id,     Line#?RECORD_TYPE.album_id},
              {aid,    Line#?RECORD_TYPE.artist_id},
              {path,   Line#?RECORD_TYPE.albumpath},
              {image,  Line#?RECORD_TYPE.image},
              {album,  Line#?RECORD_TYPE.album}]}
            || Line <- Lines],
    JsonData = {obj, [{data, Data}]},
    rfc4627:encode(JsonData).

handle('GET', _Arg) ->
    io:format("~n ~p:~p GET Request ~n", [?MODULE, ?LINE]),
    Records = do(qlc:q([X || X <- mnesia:table(?RECORD_TYPE)])),
    Json = convert_to_json(Records),
    io:format("~n ~p:~p GET Request Response ~p ~n", [?MODULE, ?LINE, Json]),
    {html, Json}.

do(Q)->
    F = fun() ->
                qlc:e(Q)
        end,
    {atomic, Value} = mnesia:transaction(F),
    Value.

和 test.yaws 文件:

<html>
<head>
  <meta charset="utf-8">
</head>
<body>
<erl>
out(Arg) ->
    Method = method(Arg) ,
    io:format("~p:~p ~p Request ~n", [?MODULE, ?LINE, Method]),
    my_json:handle(Method, Arg).

method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.
</erl>
</body>
</html>

但是我在 YAWS 输出中得到二进制信息:

{"data":[{"id":8,"aid":3,"path":[[47,114,111,111,116,47,101,114,108,97,110,103,47,116,101,115,116,108,105,115,116,47,65,114,116,105,115,116,32,78,117,109,98,101,114,51,47,65,108,98,117,109,32,78,117,109,98,101,114,32,50]],"image":[[102,114,111,110,116,46,106,112,103]],"album":[65,108,98,117,109,32,78,117,109,98,101,114,32,50]},{"id":2,"aid":1,"path":[[47,114,111,111,116,47,101,114,108,97,110,103,47,116,101,115,116,108,105,115,116,47,65,114,116,105,115,116,32,78,117,109,98,101,114,49,47,65,108,98,117,109,32,78,117,109,98,101,114,32,50]],"image":[[99,111,118,101,114,46,112,110,103]],"album":[65,108,98,117,109,32,78,117,109,98,101,114,32,50]},{"id":14,"aid":5,"path":[[47,114,111,111,116,47,101,114,108,97,110,103,47,116,101,115,116,108,105,115,116,47,65,114,116,105,115,116,32,78,117,109,98,101,114,53,47,65,108,98,117,109,32,78,117,109,98,101,114,32,50]],"image":[],"album":[65,108,98,117,109,32,78,117,109,98,101,114,32,50]},{"id":12,"aid":4,"path": ..............

如何将其转换为字符串?

附注抱歉我的英语不好,感谢您的帮助。

最佳答案

Erlang 中的字符串是不明确的,因为有两种方法来表示它们:作为二进制 (<<"foo">>),或作为整数列表 ("bar"= [$b, $a, $r ])。

您遇到的问题与第二种形式的字符串(整数列表)有关。您使用的 JSON 编码器不知道您提供的列表是否是字符串。

通常 JSON 编码器会决定字符串的特定格式。 jiffy 要求您对字符串使用二进制文件。 mochijson 采用另一种方式,要求您​​使用元组/原子 ({array, [1, 2, 3]}) 标记数组。

我的猜测是,您正在使用的模块需要字符串的二进制文件(或至少对于大字符串)。

这应该会为您修复输出:

convert_to_json(Lines) ->
Data = [{obj,
         [{id,     Line#?RECORD_TYPE.album_id},
          {aid,    Line#?RECORD_TYPE.artist_id},
          {path,   unicode:characters_to_binary(Line#?RECORD_TYPE.albumpath)},
          {image,  Line#?RECORD_TYPE.image},
          {album,  Line#?RECORD_TYPE.album}]}
        || Line <- Lines],
JsonData = {obj, [{data, Data}]},
rfc4627:encode(JsonData).

注意 unicode:characters_to_binary 的使用。这将采用 iolist(用于在文件描述符或套接字上输出的二进制或列表/二进制/整数的列表)并使用 UTF8 编码将其转换为平面二进制(它也假设 utf8 作为输入,所以要小心)。

请参阅unicode:characters_to_binary/1,2 documentation了解更多信息。

关于json - Erlang - 转换二进制可读UTF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20615939/

相关文章:

erlang - 在 Nitrogen Web 服务器中设置文件下载的内容 header

erlang - 使用 Erlydtl 进行主/应用程序布局

ssl - Erlang 从 pem 文件生成 rsa key

javascript - 在 JQuery 函数中回显 sql 查询

json - 如何在Go中直接打印从http.Get获取的json到http.ResponseWriter?

variables - Dyn_Variable 的 Tsung 问题

erlang - 运行雅司病时加载新的雅司病配置文件

webserver - 如何在 Mac OSX 上安装 YAWS

c# - 使用命名空间将 Json 转换为 XML 字符串?

java - 如何打印 json 字符串?