2

I want to translate to web by YAWS info from mnesia database. Now i have code:

-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.

and test.yaws file:

<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>

But i get binary info in YAWS output:

{"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": ..............

How i can convert this to strings?

P.S. sorry for my english and thanks for any help.

4

1 回答 1

3

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 文档

于 2013-12-16T18:37:37.720 回答