在最近的一個項目香港專業教育學院的工作,我們都處理來自EXT JS前端應用程序的大JSON數據結構。JSON對象的一個例子是這裏的下方(這是JSON的只是骨架):
{
"presence_token":"734737328233HDHSBSHSYEYEYWYWGWE",
"presence_time":"HH:Mins:Secs",
"friend_requests":
[
{
"from":"Username",
"type":"buddy",
"date":"DD/MM/YY",
"time":"HH:Mins:Secs",
"name":"Your Full name",
"email":"[email protected]"
}
],
"group_status":
[
{
"group_name":"ecampus",
"status":"running",
"members":["phil","josh","shazz"],
"start_date":"DD/MM/YY",
"start_time":"HH:Mins:Secs"
},
{
"group_name":"buganda",
"status":"off"
}
],
"friend_status":
[
{
"friend":"Friend_username",
"status":"online",
"log_on_time":"HH:Mins:Secs",
"state":"available",
"name":"Friend_Fullname",
"email":"[email protected]"
},
{
"friend":"Friend_username",
"status":"offline",
"name":"Friend_Fullname",
"email":"[email protected]"
}
]
}
後mochijson2:decode/1
,該結構物我有看起來像這樣:
{struct,[{<<"presence_token">>,
<<"734737328233HDHSBSHSYEYEYWYWGWE">>},
{<<"presence_time">>,<<"HH:Mins:Secs">>},
{<<"friend_requests">>,
[{struct,[{<<"from">>,<<"Username">>},
{<<"type">>,<<"buddy">>},
{<<"date">>,<<"DD/MM/YY">>},
{<<"time">>,<<"HH:Mins:Secs">>},
{<<"name">>,<<"Your Full name">>},
{<<"email">>,<<"[email protected]">>}]}]},
{<<"group_status">>,
[{struct,[{<<"group_name">>,<<"ecampus">>},
{<<"status">>,<<"running">>},
{<<"members">>,[<<"phil">>,<<"josh">>,<<"shazz">>]},
{<<"start_date">>,<<"DD/MM/YY">>},
{<<"start_time">>,<<"HH:Mins:Secs">>}]},
{struct,[{<<"group_name">>,<<"buganda">>},
{<<"status">>,<<"off">>}]}]},
{<<"friend_status">>,
[{struct,[{<<"friend">>,<<"Friend_username">>},
{<<"status">>,<<"online">>},
{<<"log_on_time">>,<<"HH:Mins:Secs">>},
{<<"state">>,<<"available">>},
{<<"name">>,<<"Friend_Fullname">>},
{<<"email">>,<<"[email protected]">>}]},
{struct,[{<<"friend">>,<<"Friend_username">>},
{<<"status">>,<<"offline">>},
{<<"name">>,<<"Friend_Fullname">>},
{<<"email">>,<<"[email protected]">>}]}]}]}
現在我決定創建一個模塊將這個結構轉換爲一個「深」的proplist,這個模塊將包含一個函數struct:all_keys/1
,如果我給它提供結構對象,它會以有組織的方式生成列表和元組。下面是代碼:
-module(struct).
-export([all_keys/1]).
is_struct({struct,_}) -> true;
is_struct(_) -> false.
to_binary(S) when is_list(S)-> list_to_binary(S);
to_binary(S) when is_integer(S)-> S;
to_binary(S) when is_atom(S)-> to_binary(atom_to_list(S));
to_binary(S) -> S.
to_value(V) when is_binary(V)-> binary_to_list(V);
to_value(V) when is_integer(V)-> V;
to_value(V) when is_list(V)->
try list_to_integer(V) of
PP -> PP
catch
_:_ ->
try list_to_float(V) of
PP2 -> PP2
catch
_:_ -> V
end
end;
to_value(A)-> A.
to_value2({struct,L})->
all_keys({struct,L});
to_value2([{struct,_L}|_Rest] = LL)->
[all_keys(XX) || XX <- LL];
to_value2(D) when is_binary(D)-> to_value(D);
to_value2(D) when is_list(D)->
[to_value2(Any) || Any <- D].
all_keys({struct,L})->
[{to_value(Key),to_value2(Val)} || {Key,Val} <- L];
all_keys(List)-> [all_keys(X) || X <- List].
現在,調用struct:all_keys(Struct_object)
會給這個輸出:
[{"presence_token",P_token},
{"presence_time",P_time},
{"friend_requests",
[[{"from","Username"},
{"type","buddy"},
{"date","DD/MM/YY"},
{"time","HH:Mins:Secs"},
{"name","Your Full name"},
{"email","[email protected]"}]]},
{"group_status",
[[{"group_name","ecampus"},
{"status","running"},
{"members",["phil","josh","shazz"]},
{"start_date","DD/MM/YY"},
{"start_time","HH:Mins:Secs"}],
[{"group_name","buganda"},{"status","off"}]]},
{"friend_status",
[[{"friend","Friend_username"},
{"status","online"},
{"log_on_time","HH:Mins:Secs"},
{"state","available"},
{"name","Friend_Fullname"},
{"email","[email protected]"}],
[{"friend","Friend_username"},
{"status","offline"},
{"name","Friend_Fullname"},
{"email","[email protected]"}]]}]
以上proplist這樣更容易與工作比結構對象。但是,您可能會發現另一個版本的結構模塊,特別是在着名的mochiweb示例中,這個示例名爲粘滯便箋,其鏈接我現在沒有。上面粘貼的結構模塊應該能夠幫助您使用mochijson2。 成功
你能發佈一個實際的例子時間表,顯示你的問題? – DefLog 2011-06-10 18:20:20