1. 基础用法,单个转换
Obj 结构如下:
{obj,[{name,jack},{age,20}]}
基本用法:
fc4627:encode(Obj).
例子:
12> Obj = {obj,[{name,jack},{age,20}]}.
{obj,[{name,jack},{age,20}]}
13> Json = rfc4627:encode(Obj).
"{\"name\":\"jack\",\"age\":20}"
14> io:format(Json).
{"name":"jack","age":20}ok
2.处理一组Obj:
Obj list 结构:
{obj,[{lists,[{obj,[{name,jack},{age,20}]},{obj,[{name,tony},{age,21}]}]}]}
例子:
19> A = {obj,[{name,jack},{age,20}]}.
{obj,[{name,jack},{age,20}]}
20> B = {obj,[{name,tony},{age,21}]}.
{obj,[{name,tony},{age,21}]}
21> List = {obj, [{lists, [A, B]}]}.
{obj,[{lists,[{obj,[{name,jack},{age,20}]},
{obj,[{name,tony},{age,21}]}]}]}
23> Json = rfc4627:encode(List).
"{\"lists\":[{\"name\":\"jack\",\"age\":20},{\"name\":\"tony\",\"age\":21}]}"
24> io:format(Json).
{“lists":[{"name":"jack","age":20},{"name":"tony","age":21}]}