[英]How to Convert a list of tuples into a Json string
I have a Erlang list of tuples as follows: 我有一个Erlang的元组列表如下:
[ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} ,
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]} ]
I wanted this list of tuples in this form: 我想要这种形式的元组列表:
<<" [ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} ,
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]}] ">>
So I tried using JSON parsing libraries in erlang (both jiffy and jsx ) Here is what I did: 所以我尝试在erlang中使用JSON解析库(jiffy和jsx)这是我做的:
A=[ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} ,
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]} ],
B=erlang:iolist_to_binary(io_lib:write(A)),
jsx:encode(B).
and I get the following output(here I have changed the list to binary since jsx accepts binary): 我得到以下输出(这里我已经将列表更改为二进制,因为jsx接受二进制):
<<"[{{[97]},[2],[{3,[98]},{4,[99]}],[5,[100]],[1,1],{e},[[102]]},{{[103]},
[3],[{6,[104]},{7,[105]}],[{8,[106]}],[1,1,1],{k},[[76]]}]">>
jiffy:encode(B) also gives the same output. jiffy:encode(B)也给出相同的输出。 Can anyone help me to get the output as : 任何人都可以帮助我获得输出:
<<" [ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} ,
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]}] ">>
instead of 代替
<<"[{{[97]},[2],[{3,[98]},{4,[99]}],[5,[100]],[1,1],{e},[[102]]},{{[103]},
[3],[{6,[104]},{7,[105]}],[{8,[106]}],[1,1,1],{k},[[76]]}]">>
Thank you in advance 先感谢您
Instead of io_lib:write(A)
, use io_lib:format("~p", [A])
. 而不是io_lib:write(A)
,使用io_lib:format("~p", [A])
。 It tries to guess which lists are actually meant to be strings. 它试图猜测哪些列表实际上是字符串。 (In Erlang, strings are actually lists of integers. Try it: "A" == [65]
) (在Erlang中,字符串实际上是整数列表。试一试: "A" == [65]
)
> A=[ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} ,
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]} ].
[{{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]},
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]}]
> B = erlang:iolist_to_binary(io_lib:format("~p", [A])).
<<"[{{\"a\"},[2],[{3,\"b\"},{4,\"c\"}],[5,\"d\"],[1,1],{e},[\"f\"]},\n {{\"g\"},[3],[{6,\"h\"},{7,\"i\"}],[{8,\"j\"}],[1,1,1],{k},[\"L\"]}]">>
If you don't want to see the backslashes before the double quotes, you can print the string to standard output: 如果您不想在双引号之前看到反斜杠,可以将字符串打印到标准输出:
> io:format("~s\n", [B]).
[{{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]},
{{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]}]
<<" [ {{"a"},[2],[{3,"b"},{4,"c"}],[5,"d"],[1,1],{e},["f"]} , {{"g"},[3],[{6,"h"},{7,"i"}],[{8,"j"}],[1,1,1],{k},["L"]}] ">>
This ^^ isn't a valid erlang term, but I think what you're getting at is that you want the "listy" strings, like "a"
to be printed out like "a"
instead of [97]
. 这个^^不是一个有效的erlang术语,但我认为你得到的是你想要的“listy”字符串,比如"a"
打印出像"a"
而不是[97]
。 Unfortunately, I've found this to be a serious shortcoming of Erlang. 不幸的是,我发现这是Erlang的一个严重缺点。 The problem is that the string literal "a"
is only syntactic sugar and is identical to the term [97]
, so any time you output it, you're subject to the vagaries of "is this thing a string or a list of integers?" 问题是字符串文字"a"
只是语法糖并且与术语[97]
相同,所以每当你输出它时,你都会受到这样的变化:“这是一个字符串还是一个整数列表?” The best way I know to get out of that is to use binaries as your strings wherever possible, like <<"a">>
instead of "a"
. 我知道最好的方法是尽可能使用二进制文件作为字符串,例如<<"a">>
而不是"a"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.