mochijson2 examples!

12,840

Solution 1

Yeah, I had to spend a bunch of time in the source code to figure out what was going on. Actually, that describes a lot of my experience with Erlang. This has gotten me by, generating the JSON I need. Here's a quick example.

ERL  :: {struct, [{strKey, <<"strVal">>}, {intKey, 10}, {arrayKey, [1, 2, 3]}]}
JSON :: {strKey:"strVal", intKey:10, arrayKey:[1, 2, 3]}

So in that example you can see how to make objects (which mochijson2 wants you to call structs), strings, integers and arrays. Good luck!

Solution 2

I suggest reading/watching/downloading Start Developing Web Applications on Erlang

Solution 3

Here are the equivalent erlang commands in addition to @rik.the.vik's comment:

erl -pa path/to/mochiweb/ebin
...
iolist_to_binary(mochijson2:encode({struct, [{strKey, <<"strVal">>}, {intKey, 10}, {arrayKey, [1, 2, 3]}]})).
mochijson2:decode(<<"{\"strKey\":\"strVal\", \"intKey\":10, \"arrayKey\":[1, 2, 3]}">>).
Share:
12,840
ErJab
Author by

ErJab

Updated on June 04, 2022

Comments

  • ErJab
    ErJab almost 2 years

    I'm a two-week old infant with regards to Erlang and Mochiweb. Earlier I had a system running on PHP and soon I realised that it wasn't going to be able to handle the kind of load I was expecting. So I decided to switch the backend to a Mochiweb based server. Right now I need to know how to implement JSON with Mochiweb. I'm fully aware of the existense of mochijson2 library, but being a beginner, I can't get around to figuring out how to use it. Could someone point me to some place where I can find examples of using this library or any other json library in erlang?