How to use mochijson to encode data structure in erlang? How to use mochijson to encode data structure in erlang? json json

How to use mochijson to encode data structure in erlang?


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

Video Tutorial To Start Developing Web Applications on Erlang


T6={struct, [{hello,"asdf"},{from,"1"},{to, {a,"aa"}} ]}.

should instead be:

T6={struct, [{hello,"asdf"},{from,"1"},{to, {struct, [{a,"aa"}]}} ]}.

The nested structures need to have the same "struct" style as the top-level object.