如何使用mochijson来编码erlang中的数据结构? [英] How to use mochijson to encode data structure in erlang?

查看:176
本文介绍了如何使用mochijson来编码erlang中的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mochiweb,我不知道如何使用它的json编码器来处理复杂的数据结构。 mochijson和mochijson2有什么区别?有什么好的例子吗?我总是收到以下错误:

  46> T6 = {结构,[{你好 ASDF},{从 1},{到,{A, AA}}]}。 
{struct,[{hello,asdf},{from,1},{to,{a,aa}}}}
47> mochijson2:编码(T6)。
**异常退出:{json_encode,{bad_term,{a,aa}}}函数mochijson2中的
:json_encode / 2
从mochijson2调用:' - json_encode_proplist / 2- fun-0 - '/ 3
从列表中调用:foldl / 3
从mochijson2调用:json_encode_proplist / 2


39> T3 = {结构,[{你好 ASDF},{[{从 1},{到, 2}]}]}。
{struct,[{hello,asdf},{[{from,1},{to,2}]}}}
40> mochijson:编码(T3)。
**异常错误:no function子句匹配mochijson:' - json_encode_proplist / 2-fun-0 - '({[{from,1},{to,2}]},
[44,\asdf\,58,\hello\,123],
{encoder,unicode,null})
在函数列表中:foldl / 3
来自mochijson的电话:json_encode_proplist / 2


解决方案

mochijson2使用字符串作为二进制文件,列表作为数组,仅解码UTF-8。 mochijson对unicode代码点进行解码和编码。



要创建深层结构,我做了以下操作:

  2& ; L = {struct,[{key2,[192]}]}。 
{struct,[{key2,?}]}
3>
3> L2 = {struct,[{key,L}]}。
{struct,[{key,{struct,[{key2,?}]}}}}
4>
4> mochijson:编码(L2)。
[123,\key\,58,
[123,\key2 \,58,[34,\\0000 ],125],
125]

所以如果你使用列表递归创建你的数据结构那么你会没事的。我不知道为什么深层结构不受支持,但它们似乎不是,至少不是你试图创建它们的方式。也许别人知道一个更聪明的方式来做到这一点。



你也可以查看这个线程: mochijson2示例!





http://beebole.com/en/blog/erlang/tutorial-web-application- erlang /


I am using mochiweb and I don't know how to use its json encoder to deal with complex data structure. What's the differences between mochijson and mochijson2? Is there any good example? I always get the following errors:

46> T6={struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}.
{struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}
47> mochijson2:encode(T6).                                
** exception exit: {json_encode,{bad_term,{a,"aa"}}}
     in function  mochijson2:json_encode/2
     in call from mochijson2:'-json_encode_proplist/2-fun-0-'/3
     in call from lists:foldl/3
     in call from mochijson2:json_encode_proplist/2


39> T3={struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}.
{struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}
40> mochijson:encode(T3).                                 
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'({[{from,"1"},{to,"2"}]},
                                                                                           [44,"\"asdf\"",58,"\"hello\"",123],
                                                                                           {encoder,unicode,null})
     in function  lists:foldl/3
     in call from mochijson:json_encode_proplist/2

解决方案

mochijson2 works with strings as binaries, lists as arrays, and only decodes UTF-8. mochijson decodes and encodes unicode code points.

To create a deep struct I did the following:

2> L = {struct, [{key2, [192]}]}. 
{struct,[{key2,"?"}]}
3> 
3> L2 = {struct, [{key, L}]}.   
{struct,[{key,{struct,[{key2,"?"}]}}]}
4> 
4> mochijson:encode(L2).
[123,"\"key\"",58,
 [123,"\"key2\"",58,[34,"\\u00c0",34],125],
 125]

So if you recursively create your data structure using lists then you'll be fine. I'm not sure why deep structs aren't supported, but they don't seem to be, at least not the way you're trying to create them. Maybe someone else knows a more clever way to do this.

You can also check out this thread: mochijson2 examples!

or

http://beebole.com/en/blog/erlang/tutorial-web-application-erlang/

这篇关于如何使用mochijson来编码erlang中的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆