在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