问题描述
我使用mochijson2对一些数据进行编码。但是我发现它在字符串中表现为奇怪的列表。
示例:
其中102,111,111是$ f,$ o,$ o编码为字符串
44是逗号,91和93是方形的。
当然,如果我输出这个地方,我会得到字符串[102,111,111],这显然不是什么我是什么。
如果我尝试
所以我再次得到一个两个双引号和二进制部分的列表,其中可以使用list_to_binary / 1将其转换为二进制
这是一个问题 - 为什么这么一致。我知道有一个问题distingushing erlang列表应该被编码为json数组和erlang字符串应该被编码为json字符串,但至少可以输出二进制文件,当我传递二进制文件
第二个问题:
看起来mochijson输出的东西都很好(因为它使用特殊的元组来指定数组{array,...})
mochijson2和mochijson有什么区别?性能? Unicode处理?还有其他什么?
谢谢
我的猜测是决定在mochijson中,它将二进制文件视为字符串,并将整数列表作为整数列表。 (Un?)幸运的是,Erlang中的字符串实际上是整数列表。
因此,您的foo或换句话说,您的[102,111,111]被翻译代表[102,111,111]的文字。在第二种情况下,您的<foo>>字符串变为foo
关于第二个问题,mochijson似乎总是返回一个字符串,而mochijson2返回类型。 Iodata基本上是一个递归的字符串,二进制和iodatas列表(实际上是iolists)。如果您只打算通过电子邮件发送结果,将其嵌入列表中将其转换为扁平字符串更为有效。
I'm encoding some data using mochijson2.But I found that it behaves strange on strings as lists.
Example:
Where "102", "111", "111" are $f, $o, $o encoded as strings44 are commas and 91 and 93 are square brakets.
Of course if I output this somewhere I'll get string "[102,111,111]" which is obviously not that what I what.
If i try
So I again i get a list of two doublequotes and binary part within which can be translated to binary with list_to_binary/1
Here is the question - why is it so inconsistent. I understand that there is a problem distingushing erlang list that should be encoded as json array and erlang string which should be encoded as json string, but at least can it output binary when i pass it binary?
And the second question:Looks like mochijson outputs everything nice (cause it uses special tuple to designate arrays {array, ...})
What's the difference between mochijson2 and mochijson? Performance? Unicode handling? Anything else?
Thanks
My guess is that the decision in mochijson is that it treats a binary as a string, and it treats a list of integers as a list of integers. (Un?)fortunately strings in Erlang are in fact a list of integers.
As a result your "foo", or in other words, your [102,111,111] is translated into text representing "[102,111,111]". In the second case your <<"foo">> string becomes "foo"
Regarding the second question, mochijson seems to always return a string, whereas mochijson2 returns an iodata type. Iodata is basically a recursive list of strings, binaries and iodatas (in fact iolists). If you only intend to send the result "through the wire", it is more efficient to just nest them in a list than convert them to a flat string.
这篇关于mochijson2或mochijson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!