JSON and JSON-RPC for Erlang
下載解壓編譯完之後,輸出的beam文件在ebin目錄下~~
上面下載的包裡有一個rfc4627模塊,提供了JSON數據的encode、decode等方法,還可以將JSON與Erlang Record相互轉換,十分方便快捷,下面看看用法:
首先試試將字符串轉換為Json Object: 1> JsonBin = <<"{ 1> \"username\":\"Oscar Tong\", 1> \"password\":\"123456\", 1> \"male\":true, 1> \"age\":23 1> }">>. <<"{\n\"username\":\"Oscar Tong\",\n\"password\":\"123456\",\n\"male\":true,\n\"age\":23\n}">> 2> {ok, {obj, JsonObj}, _Rest} = rfc4627:decode(JsonBin). {ok,{obj,[{"username",<<"Oscar Tong">>}, {"password",<<"123456">>}, {"male",true}, {"age",23}]}, []} 再轉換為JSON格式的字符串: 3> rfc4627:encode({obj,JsonObj}). "{\"username\":\"Oscar Tong\",\"password\":\"123456\",\"male\":true,\"age\":23}"經測試表明,JSON數據的解析與處理速度比XML要快得多,decode和encode僅耗時約40us,比xmerl模塊處理XML需要超過200us相比實在是快多了~~
0 意見