1> erlang 转换为 json
1> mochijson:encode(1) 整数
1> .
"1"
2> mochijson:encode(true) true
2> .
"true"
3> mochijson:encode(false) flse
3> .
"false"
4> mochijson:encode(null) null
4> .
"null"
5> mochijson:encode(1.1) 浮点数
5> .
"1.1"
6> mochijson:encode(01.10)
6> .
"1.1"
7> mochijson:encode(sdd) atom
7> .
"\"sdd\""
8> mochijson:encode("string") list . 是 "string", 不是[1]
8> .
"\"string\""
9> mochijson:encode([list])
9> .
** exception exit: {json_encode,{bad_char,list}}
in function mochijson:json_encode_string_unicode_1/1
in call from mochijson:json_encode_string_unicode/1
10> is_list([list]).
true
11> mochijson:encode(<<binary>>)
11> .
** exception error: bad argument
12> mochijson:encode(<<"binary">>)
12> .
"\"binary\""
13> mochijson:encode({array,[list]}) list
13> .
[91,"\"list\"",93]
14> mochijson:encode({array,[list,list2]})
14> .
[91,"\"list\"",44,"\"list2\"",93]
15> mochijson:encode({struct,[list,list2]})
15> .
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'(list,"{",{encoder,unicode,null})
in function lists:foldl/3
in call from mochijson:json_encode_proplist/2
16> mochijson:encode({struct,["list","list2"]})
16> .
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'("list","{",{encoder,unicode,null})
in function lists:foldl/3
in call from mochijson:json_encode_proplist/2
17> mochijson:encode({struct,[{list,"list"},{"list2"}]}) [{k,v},{k1,v1}]
17> .
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'({"list2"},
[44,"\"list\"",58,"\"list\"",123],
{encoder,unicode,null})
in function lists:foldl/3
in call from mochijson:json_encode_proplist/2
18> mochijson:encode({struct,[{list,"list"},{"list2",list}]})
18> .
[123,"\"list\"",58,"\"list\"",44,"\"list2\"",58,"\"list\"",
125]
19>
mochijson.erl代码片段
108
109 json_encode(true, _State) ->
110 "true";
111 json_encode(false, _State) ->
112 "false";
113 json_encode(null, _State) ->
114 "null";
115 json_encode(I, _State) when is_integer(I) ->
116 integer_to_list(I);
117 json_encode(F, _State) when is_float(F) ->
118 mochinum:digits(F);
119 json_encode(L, State) when is_list(L); is_binary(L); is_atom(L) ->
120 json_encode_string(L, State);
121 json_encode({array, Props}, State) when is_list(Props) ->
122 json_encode_array(Props, State);
123 json_encode({struct, Props}, State) when is_list(Props) ->
124 json_encode_proplist(Props, State);
125 json_encode(Bad, #encoder{handler=null}) ->
126 exit({json_encode, {bad_term, Bad}});
127 json_encode(Bad, State=#encoder{handler=Handler}) ->
128 json_encode(Handler(Bad), State).
129
decode:
19>
19> v(18)
19> .
[123,"\"list\"",58,"\"list\"",44,"\"list2\"",58,"\"list\"",
125]
20> mochijson:decode(v(18))
20> .
{struct,[{"list","list"},{"list2","list"}]}
21> mochijson:decode(v(14))
21> .
{array,["list","list2"]}
22> mochijson:decode(v(13))
22> .
{array,["list"]}
23> mochijson:decode(v(12))
23> .
"binary"
24> mochijson:decode(v(8))
24> .
"string"
25> mochijson:decode(v(7))
25> .
"sdd"
26> mochijson:decode(v(6))
26> .
1.1
------------------------------------------------------------------------------------------
(trends@jason-lxw)2> mochijson:decode("{\"result\":\"200\"}").
{struct,[{"result","200"}]}
(trends@jason-lxw)1> mochijson:decode({"result":{"200":"ss"}}).
* 2: illegal expression
(trends@jason-lxw)2> mochijson:decode("{\"result\":{\"200\":\"ss\"}}").
{struct,[{"result",{struct,[{"200","ss"}]}}]}
(trends@jason-lxw)3> mochijson:decode("{ \"firstName\": \"Brett\", \"lastName\":\"McLaughlin\", \"email\": \"aaaa\" }").
{struct,[{"firstName","Brett"},
{"lastName","McLaughlin"},
{"email","aaaa"}]}
in call from mochijson:json_decode/2
(trends@jason-lxw)20> mochijson:decode("{\"pel\":\"sdf\"}")
(trends@jason-lxw)20> .
{struct,[{"pel","sdf"}]}
(trends@jason-lxw)21> mochijson:decode("{\"pel\":[\"sdf\"]}")
(trends@jason-lxw)21> .
{struct,[{"pel",{array,["sdf"]}}]}
(trends@jason-lxw)22> mochijson:decode("{\"pel\":[\"sdf\", \"ss\"]}")
(trends@jason-lxw)22> .
{struct,[{"pel",{array,["sdf","ss"]}}]}
(trends@jason-lxw)23> mochijson:decode("{\"pel\":[{ \"firstName\":\"Brett\", \"lastName\":\"McLaughlin\", \"email\":\"aaaa\" }"]}")
(trends@jason-lxw)23> .
(trends@jason-lxw)23> ".
* 1: syntax error before: ']'
(trends@jason-lxw)23> mochijson:decode("{\"pel\":[\"sdf\",{ \"firstName\":\"Brett\", \"lastName\":\"McLaughlin\", \"email\":\"aaaa\" }"}]}")
(trends@jason-lxw)23> .
(trends@jason-lxw)23> "/.
* 1: syntax error before: '}'
(trends@jason-lxw)23> mochijson:decode("{\"pel\":[\"sdf\", {}]}")
(trends@jason-lxw)23> .
{struct,[{"pel",{array,["sdf",{struct,[]}]}}]}
(trends@jason-lxw)24> mochijson:decode("{\"pel\":[\"sdf\", {\"sdf\"}]}")
(trends@jason-lxw)24> .
** exception error: no match of right hand side value {end_object,"]}",{decoder,utf8,null,1,23,key}}
in function mochijson:decode_object/3
in call from mochijson:decode_array/3
in call from mochijson:decode_object/3
in call from mochijson:json_decode/2
(trends@jason-lxw)25> mochijson:decode("{\"pel\":[\"sdf\", {\"sdf\":\"sdf\"}]}")
(trends@jason-lxw)25> .
{struct,[{"pel",{array,["sdf",{struct,[{"sdf","sdf"}]}]}}]}
(trends@jason-lxw)26> mochijson:decode("{ \"firstName\":\"Brett\", \"lastName\":\"McLaughlin\", \"email\":\"aaaa\" }").
{struct,[{"firstName","Brett"},
{"lastName","McLaughlin"},
{"email","aaaa"}]}
(trends@jason-lxw)27> mochijson:decode("{\"pel\":[\"sdf\", {\"sdf\":\"sdf\"},"{ \"firstName\":\"Brett\", \"lastName\":\"McLaughlin\", \"email\":\"aaaa\" }"]}")
(trends@jason-lxw)27> .
(trends@jason-lxw)27> ".
* 1: syntax error before: '{'
(trends@jason-lxw)27> mochijson:decode("{\"pel\":[\"sdf\", {\"sdf\":\"sdf\"}]}") (trends@jason-lxw)27> .
{struct,[{"pel",{array,["sdf",{struct,[{"sdf","sdf"}]}]}}]}
(trends@jason-lxw)28> mochijson:decode("{\"pel\":[\"sdf\", {\"sdf\":\"sdf\"}, { \"firstName\":\"Brett\", \"lastName\":\"McLaughlin\", \"email\":\"aaaa\" }]}")
(trends@jason-lxw)28> .
{struct,[{"pel",
{array,["sdf",
{struct,[{"sdf","sdf"}]},
{struct,[{"firstName","Brett"},
{"lastName","McLaughlin"},
{"email","aaaa"}]}]}}]}
(trends@jason-lxw)29>
分享到:
相关推荐
ej_merge 该模块为JSON补丁(RFC 6902)和JSON合并补丁(RFC 7396)规范提供了纯Erlang支持。... ... ej_merge:mergepatch(mochijson2:decode(A),mochijson2:decode(B))。 参考 相关工作:
支持以下常见的Erlang数据结构: list() dict() gb_trees() proplist() {struct, proplist()} (通常在mochijson2中使用) {proplist()} ( ) map() Erlang 17+ 密钥仅允许使用以下数据类型,并且如果发生任何类型...
erljson_bench 用于比较各种JSON编码/解码库的脚本图书馆比较ejson 瞬间json jsonx jsx mochijson2要求要执行这些基准测试,路径上必须有一个有效的erlang(erl和escript)。建立和运行要下载依赖项并进行编译: ...
该项目是一款基于freeRTOS操作系统和STM32F103x微控制器的手机远程控制浴室温度系统设计源码,共包含1087个文件,包括580个C语言源文件、269个头文件、45个汇编源文件、36个数据文件、36个目标文件、35个编译规则文件、28个包含文件、27个文本文件、6个源文件、3个归档文件。此系统通过手机远程实现对浴室温度的有效控制,适用于智能浴室环境管理。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。