使用下列JSON库:
[url]http://www.lshift.net/blog/2007/02/17/json-and-json-rpc-for-erlang[/url]
该JSON库采用[url=http://www.erlang.org/ml-archive/erlang-questions/200511/msg00193.html]Joe Armstrong prefered Data type mapping[/url]
即:
[code]
JSON Obj = type obj() = {obj, [{key(), val()}]}
JSON Array = type array() = [val()]
JSON Number = type num() = int() | float()
JSON String = type str() = bin()
JSON true false null = true, false null (atoms)
With Type val() = obj() | array() | num() | str() | true | false | null
and key() being a str(). (Or a binary or atom, during JSON encoding.)
[/code]
测试如下:
[code]
Eshell V5.6.3 (abort with ^G)
1> O = rfc4627:encode({obj, [{name, hideto}, {age, 23}]}).
"{\"name\":\"hideto\",\"age\":23}"
2> rfc4627:decode(O).
{ok,{obj,[{"name",<<"hideto">>},{"age",23}]},[]}
3> A = rfc4627:encode([1,2,3,4,5]).
"[1,2,3,4,5]"
4> rfc4627:decode(A).
{ok,[1,2,3,4,5],[]}
5> N = rfc4627:encode(12345).
"12345"
6> rfc4627:decode(N).
{ok,12345,[]}
7> S = rfc4627:encode("12345").
"[49,50,51,52,53]"
8> rfc4627:decode(S).
{ok,"12345",[]}
9> T = rfc4627:encode(true).
"true"
10> rfc4627:decode(T).
{ok,true,[]}
11> F = rfc4627:encode(false).
"false"
12> rfc4627:decode(F).
{ok,false,[]}
13> Null = rfc4627:encode(null).
"null"
14> rfc4627:decode(Null).
{ok,null,[]}
[/code]
[url]http://www.lshift.net/blog/2007/02/17/json-and-json-rpc-for-erlang[/url]
该JSON库采用[url=http://www.erlang.org/ml-archive/erlang-questions/200511/msg00193.html]Joe Armstrong prefered Data type mapping[/url]
即:
[code]
JSON Obj = type obj() = {obj, [{key(), val()}]}
JSON Array = type array() = [val()]
JSON Number = type num() = int() | float()
JSON String = type str() = bin()
JSON true false null = true, false null (atoms)
With Type val() = obj() | array() | num() | str() | true | false | null
and key() being a str(). (Or a binary or atom, during JSON encoding.)
[/code]
测试如下:
[code]
Eshell V5.6.3 (abort with ^G)
1> O = rfc4627:encode({obj, [{name, hideto}, {age, 23}]}).
"{\"name\":\"hideto\",\"age\":23}"
2> rfc4627:decode(O).
{ok,{obj,[{"name",<<"hideto">>},{"age",23}]},[]}
3> A = rfc4627:encode([1,2,3,4,5]).
"[1,2,3,4,5]"
4> rfc4627:decode(A).
{ok,[1,2,3,4,5],[]}
5> N = rfc4627:encode(12345).
"12345"
6> rfc4627:decode(N).
{ok,12345,[]}
7> S = rfc4627:encode("12345").
"[49,50,51,52,53]"
8> rfc4627:decode(S).
{ok,"12345",[]}
9> T = rfc4627:encode(true).
"true"
10> rfc4627:decode(T).
{ok,true,[]}
11> F = rfc4627:encode(false).
"false"
12> rfc4627:decode(F).
{ok,false,[]}
13> Null = rfc4627:encode(null).
"null"
14> rfc4627:decode(Null).
{ok,null,[]}
[/code]
本文介绍了使用Erlang中的JSON库进行数据编码与解码的测试案例,展示了如何处理对象、数组、数字、字符串及布尔值等不同类型的JSON数据。
682

被折叠的 条评论
为什么被折叠?



