我有两个对象,我想捆绑和运输在一个数组中,使用mochijson编码为JSON。它们是:如何使用mochijson对对象数组进行编码?
> Book0 = {struct, [{"title", "a book"}, {"id", "1"}]}.
> Book1 = {struct, [{"title", "another book"}, {"id", "2"}]}.
然而,
> mochijson:encode({struct, [{"books", [Book0, Book1]}]}).
** exception exit: {json_encode,{bad_char,{struct,[{"title","a book"},
{"id","1"}]}}}
in function mochijson:json_encode_string_unicode_1/1 (src/mochijson.erl, line 203)
in call from mochijson:json_encode_string_unicode/1 (src/mochijson.erl, line 190)
in call from mochijson:'-json_encode_proplist/2-fun-0-'/3 (src/mochijson.erl, line 151)
in call from lists:foldl/3 (lists.erl, line 1197)
in call from mochijson:json_encode_proplist/2 (src/mochijson.erl, line 154)
如何构建我的数据mochijson?我知道这个article,但不幸的是,它不包括对象的数组。
最正确'mochijson2:编码({结构,[<<"books">> [Book0,第一册]]})'其中:'Book0 = {结构,[{<<"title">>,<<"a book">>},{<<"id">>,<<"1">> }}}''和'Book1 = {struct,[{“另一本书>> >>},{<<"id">>,<<"2">>}]}。'注意'mochijson2'是一个比mochijson更新的实现' – 2012-03-08 12:48:30
使用mochijson2,我们可以将'struct'关键字和JSON对象作为'{Key,Value}'的列表放入。 – shino 2012-03-08 13:58:58
如果你正在做大量的编码/解码寻找比mochiweb更快的东西,我会推荐[jiffy](https://github.com/davisp/jiffy)。这是一组使用与用于JSON的mochijson类似的结构的nifs。结构的主要区别是你不需要元组中的'struct'和'array'原子。例如)而不是上述,它只是'{[{“books”,[Book0,Book1]}]}'。 – 2012-03-09 01:30:04