Erlang Json

今天浏览了一下Erlang Json数据格式转化, 只是浏览,还没有细看, 做了小测试

rfc4627

当然主要是这个地方找到的 http://www.lshift.net/blog/2007/02/17/json-and-json-rpc-for-erlang/

  1. git clone git://github.com/tonyg/erlang-rfc4627
  2. rebar com 编译获取的项目 erlang-rfc4627
  3. 查看下 test 文档后 小小测试,当然我的测试很明显也是用的测试文档的例子
    Eshell V6.0 (abort with ^G)
    1> rfc4627:encode({obj,[{name,hello},{age,23}]}).
    "{"name":"hello","age":23}"
    2> O = rfc4627:encode({obj, [{name, hideto}, {age, 23}]}).
    "{"name":"hideto","age":23}"
    3> rfc4627:decode(O).
    {ok,{obj,[{"name",<<"hideto">>},{"age",23}]},[]}
    4>
    4> A = #address{number = 6, street = <<"Rufus Street">>, town = <<"London">>}.
  • 2: record address undefined
    5> rd(address, {number, street, town, country = <<"England">>}).
    address
    6> A = #address{number = 6, street = <<"Rufus Street">>, town = <<"London">>}.
    ‘#address{number = 6,street = <<"Rufus Street">>,
    town = <<"London">>,country = <<"England">>}
    7> ?RFC4627_FROM_RECORD(address, A).
  • 1: syntax error before: '?'
    7> rfc4627:from_record(A, RName, record_info(fields, RName)).
  • 1: variable 'RName' is unbound
    8> rfc4627:from_record(A, address, record_info(fields, RName)).
  • 1: variable 'RName' is unbound
    9> rfc4627:from_record(A, address, record_info(fields, address)).
    {obj,[{"number",6},
    {"street",<<"Rufus Street">>},
    {"town",<<"London">>},
    {"country",<<"England">>}]}
    10> R = rfc4627:from_record(A, address, record_info(fields, address)).
    {obj,[{"number",6},
    {"street",<<"Rufus Street">>},
    {"town",<<"London">>},
    {"country",<<"England">>}]}
    11> rfc4626:to_record(R, #address{}, record_info(field, address)).
  • 1: illegal record info
    12> rfc4626:to_record(R, #address{}, record_info(fields, address)).
    ** exception error: undefined function rfc4626:to_record/3
    13> rfc4627:to_record(R, #address{}, record_info(fields, address)).
    ‘#address{number = 6,street = <<"Rufus Street">>,
    town = <<"London">>,country = <<"England">>}
    14> rfc4627:equiv({obj, [{"a", true}, {"b", 123}]}, {obj, [{"a", true}, {"b", 123}]}).
    true

mochijson2

网址是: https://github.com/mochi/mochiweb

  1. git clone git://github.com/mochi/mochiweb.git
  2. rebar com 编译获取的项目 mochiweb
  3. 开始小小测试
Eshell V6.0  (abort with ^G)
1> ErlSrc =  {struct,[{<<"students">>,
1>           [{struct,[{<<"student_number">>,45},
1>                     {<<"details">>,
1>                      [{struct,[{<<"FirstName">>,<<"Joshua">>},
1>                                {<<"Surname">>,<<"Muzaaya">>}]}]}]}]}]}.
{struct,[{<<"students">>,
          [{struct,[{<<"student_number">>,45},
                    {<<"details">>,
                     [{struct,[{<<"FirstName">>,<<"Joshua">>},
                               {<<"Surname">>,<<"Muzaaya">>}]}]}]}]}]}
3> JsonDes = mochijson2:encode(ErlSrc).
[123,
 [34,<<"students">>,34],
 58,
 [91,
  [123,
   [34,<<"student_number">>,34],
   58,"45",44,
   [34,<<"details">>,34],
   58,
   [91,
    [123,
     [34,<<"FirstName">>,34],
     58,
     [34,<<"Joshua">>,34],
     44,
     [34,<<"Surname">>,34],
     58,
     [34,<<"Muzaaya">>,34],
     125],
    93],
   125],
  93],
 125]
4> mochijson2:decode(JsonDes).
{struct,[{<<"students">>,
          [{struct,[{<<"student_number">>,45},
                    {<<"details">>,
                     [{struct,[{<<"FirstName">>,<<"Joshua">>},
                               {<<"Surname">>,<<"Muzaaya">>}]}]}]}]}]}

相关资料:
Erlang的JSON库
JSON 百度百科
Json格式解析

原文地址:https://www.cnblogs.com/ShankYan/p/4141841.html