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)。建立和运行要下载依赖项并进行编译: ...
【低空经济】低空人工智能调度中心建设方案
少儿编程scratch项目源代码文件案例素材-诅咒大厦.zip
scratch少儿编程逻辑思维游戏源码-纸片马里奥 激流勇进.zip
scratch少儿编程逻辑思维游戏源码-一路跳跃.zip
内容概要:本文详细介绍了五个用于空气耦合超声仿真的COMSOL模型,涵盖二维和三维场景,适用于铝板和钢板的多种缺陷检测。每个模型都包含了具体的参数设置、边界条件选择以及优化技巧。例如,Lamb波检测模型展示了如何利用A0模态检测铝板内的气泡,而三维模型则强调了内存管理和入射角参数化扫描的重要性。表面波检测模型提供了裂纹识别的相关性分析方法,变厚度模型则展示了如何通过几何参数化来模拟复杂的工件形态。文中还分享了许多实用的操作技巧,如内存优化、信号处理和自动化检测逻辑。 适用人群:从事无损检测研究的技术人员、COMSOL软件使用者、超声检测领域的研究人员。 使用场景及目标:①帮助用户理解和掌握空气耦合超声仿真的具体实现方法;②提供实际工程应用中的缺陷检测解决方案;③指导用户进行高效的仿真建模和结果分析。 其他说明:文中提供的模型不仅涵盖了常见的缺陷检测场景,还包括了一些高级技巧,如参数化扫描、自动化检测逻辑等,能够显著提高工作效率。同时,文中还给出了硬件配置建议和一些常见的注意事项,确保用户可以顺利运行这些模型。
实训商业源码-【脐橙】租赁 2.80.0+租赁商家-毕业设计.zip
scratch少儿编程逻辑思维游戏源码-幽灵冲刺.zip
scratch少儿编程逻辑思维游戏源码-粘粘世界物理.zip
机器人开发教程&案例&相关项目资源,奖励仅
实训商业源码-酒吧微上墙4.1.0-毕业设计.zip
实训商业源码-会员计次卡V1.1.1-毕业设计.zip
实训商业源码-二手跳蚤市场V5.4.10带微信支付+上架通知+广告插件-毕业设计.zip
实训商业源码-健康保健类企业网站源码-毕业设计.zip
Linux环境安装mysql的RPM包以及安装步骤:客户端和服务端的安装
实训商业源码-房产中介小程序8.0.51+前端-毕业设计.zip
scratch少儿编程逻辑思维游戏源码-钟声.zip
实训商业源码-【最新版】Xyplayer X3.96 官方正式版-毕业设计.zip