在JSON API中使用Erlang的最佳實踐是什麼? 我的意思是你如何處理JSON - 邏輯 - 數據庫 - 邏輯 - JSON生命週期。您在Erlang應用程序中使用了哪些數據結構,工具和技術?Erlang JSON API數據生命週期
3
A
回答
7
有兩個工具我傾向於在二郎用於JSON:jsx
和jiffy
。檢查它們,因爲它們具有稍微不同的性質,靈活性和速度。
主要的問題是Erlang沒有內置的字典類型。因此,JSON對象通常表示爲屬性列表,即
{ a : 10,
b : 20 }
表示爲元組的列表:
[{a, 10}, {b, 20}]
,這是看出來了重大的事情。這也意味着應該儘可能地避免使用JSON作爲內部表示格式,因爲從長遠來看,處理起來很不方便。
5
1
你的問題比收到的答案爲止更大。但是,對於操縱JSON,他們已經告訴了你關於mochijson.erl
,mochijson2.erl
所有這些都與mochiweb
HTTP庫一起提供的信息。要使用它們,一個JSON對象被表示爲Struct
對象是這樣的:
In JSON
{"FirstName": "Joshua", "Surname": "Muzaaya"}
In Erlang
{struct,[{<<"FirstName">>,<<"Joshua">>},{<<"Surname">>,<<"Muzaaya">>}]}
解析來來回回:
JSON_DATA = {"students":[現在的數據庫,我不知道你真正想要的,但是,通常情況下,我們可以寫數據在Key Value商店中,
{"student_number":45,
"details":[{"FirstName": "Joshua", "Surname": "Muzaaya"} ] } ]}
From JSON to Erlang
$> mochijson2:decode(JSON_DATA). {struct,[{<<"students">>, [{struct,[{<<"student_number">>,45}, {<<"details">>, [{struct,[{<<"FirstName">>,<<"Joshua">>}, {<<"Surname">>,<<"Muzaaya">>}]}]}]}]}]}
From Erlang to JSON
$> mochijson2:encode(StructObject).
Mnesia
或
CouchDB
經由
RESTFUL
訪問API。我在Erlang寫了一個
Couchbase Single Server
客戶端。這裏有一些裏面的功能:
create_database(ServerDomain,DBName)-> try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type",
"application/json"}],put,[]) of
{ok,_,_,Result} -> case proplists:get_value(<<"ok">>,element(2,mochijson2:decode(Result
))) of undefined -> {error,failed}; _ -> ok end; Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
delete_database(ServerDomain,DBName)-> try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type","application/json"}],delete,[]) of {ok,_,_,Result} -> case proplists:get_value(<<"ok">>,element(2,mochijson2:decode(Result))) of undefined -> {error,failed}; _ -> ok end; Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
%% read/3 ::= Struct
read(ServerDomain,DBName,DocId)-> try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId,[{"Content-Type","application/json"}],get,[]) of
{ok,_,_,Result} -> mochijson2:decode(Result);
Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
%% write/3 ::= [{id,Id::string()},{rev,Rev::string()}]
write(ServerDomain,DBName,DocStruct)-> try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type","application/json"}],post,lists:flatten(mochijson:encode(DocStruct))) of
{ok,_,_,Result} -> E = element(2,mochijson2:decode(Result)), case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"rev">>,E)} of {undefined,_} -> {error,E}; {_,undefined} -> {error,E}; {Id,Rev} -> [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}]
end;
Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
%% remember to specify the '_rev' of recent copy when updating %% update/4 ::= [{id,Id::string()},{rev,Rev::string()}] update(ServerDomain,DBName,DocId,DocStruct)-> try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId,[{"Content-Type","application/json"}],put,lists:flatten(mochijson:encode(DocStruct))) of
{ok,_,_,Result} -> E = element(2,mochijson2:decode(Result)), case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"rev">>,E)} of {undefined,_} -> {error,E}; {_,undefined} -> {error,E}; {Id,Rev} -> [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}]
end;
Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
%% you need to provide the '_rev' of current copy %% of doc to be deleted
delete(ServerDomain,DBName,DocId,Rev)-> try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId ++ "?rev=" ++ Rev,[{"Content-Type","application/json"}],delete,[]) of
{ok,_,_,Result} -> E = element(2,mochijson2:decode(Result)), case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"ok">>,E)} of {undefined,_} -> {error,E}; {_,undefined} -> {error,E}; {_Id,_} -> ok
end;
Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
random(ServerDomain,Count) when is_integer(Count)-> try ibrowse:send_req(ServerDomain ++ "_uuids?count=" ++ integer_to_list(Count),[{"Content-Type","application/json"}],get,[]) of {ok,_,_,Result} -> [binary_to_list(Z) || Z <- proplists:get_value(<<"uuids">>,element(2,mochijson2:decode(Result)))]; Any -> {error,Any} catch EE:EE2 -> {error,{EE,EE2}} end.
所以,也許嘗試打破你的問題。否則,你可以在上面看到我正在玩JSON。要使用abpve的方法,請先使用戶ibrowse
正在運行。
相關問題
- 1. React組件生命週期API請求
- 2. Android數據庫高級生命週期
- 3. 流星數據訂閱生命週期
- 4. 活動生命週期和數據庫
- 5. Ember-數據模型生命週期
- 6. mvc json請求生命週期
- 7. Graph API參考的生命週期?
- 8. servlet生命週期
- 9. JUnit生命週期
- 10. popToViewController生命週期
- 11. JSF2.0生命週期
- 12. ScheduledExecutorService生命週期?
- 13. BroadcastReceiver生命週期
- 14. BroadcastReceiver生命週期
- 15. UIControl生命週期
- 16. PreferenceActivity生命週期
- 17. System.Timers.Timer生命週期
- 18. viewController生命週期
- 19. SPListItem生命週期
- 20. ASP.NET:HttpApplication生命週期
- 21. MarshalByRefObject生命週期
- 22. IntentService生命週期
- 23. Memcache生命週期
- 24. X509Certificates生命週期
- 25. JVM生命週期?
- 26. UINavigationItem生命週期
- 27. WearableListenerService生命週期
- 28. Eclipse生命週期
- 29. Doctrine2。生成生命週期
- 30. 生命週期函數的命名
另一種選擇是'mochisjon2',它附帶[Mochiweb](https://github.com/mochi/mochiweb)。 [Webmachine](https://github.com/basho/webmachine)基於Mochiweb。 – Tilman
現在呢! Erlang 17 –
@ArthurColléErlang 18,不,它沒有,是的,它有地圖,但它沒有地圖:to_json/1。 – Aus