【发布时间】:2013-12-17 15:32:58
【问题描述】:
我想通过来自 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 中的字符串是整数列表。见stackoverflow.com/questions/10043141/…
-
您的问题似乎与您标题中的问题相反。你有一个字符串(在Erlang中它只是一个整数列表),但是你的JSON生成库似乎需要一个二进制文件。
-
感谢您的澄清,我更正了主题。