我有兩個對象,我想捆綁和運輸在一個數組中,使用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