Rendering JSON from erlyweb controllers
Code below illustrates how to render JSON and raw HTML/TEXT from a controller.
-module(mock_controller).
-export([index/1, mochi_test/1, lshift_test/1, yaws_test/1]).
index(A) ->
{ response, [ { html, <<"<b>hello json</b>">> } ] }. %% renders raw html code
%% encoding a tuple
mochi_test(A) ->
{ response, [ { html, mochijson2:encode({struct, [{'mochi', 'json2'}]}) } ] }. %% mochiweb JSON implementation
lshift_test(A) ->
{ response, [ { html, rfc4627:encode({obj, [{'lshift','rfc4627'}]})} ] }. %% Lshift implementation of JSON
yaws_test(A) ->
{ response, [ { html, json:encode({struct, [{"yaws", "json"}]}) } ] }. %% YAWS json
You need to download the JSON implementation and place it in the ’src’ directory of your Erlyweb application
directory. Lshift and Mochi JSON modules.
Loading...