Erlang Json

今天瀏覽了一下Erlang Json數據格式轉化, 只是瀏覽,尚未細看, 作了小測試

rfc4627

固然主要是這個地方找到的 http://www.lshift.net/blog/2007/02/17/json-and-json-rpc-for-erlang/html

  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}]}).
    truegit

    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格式解析
相關文章
相關標籤/搜索