`
langzhe
  • 浏览: 288182 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mochijson小计

 
阅读更多

 

 

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> 





 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics