0

使用 mochijson2 解码 JSON 数据后,我得到了这个结构化的 proplist。

 {struct,
 [{<<"query">>,
  {struct,
  [{<<"count">>,4},
   {<<"created">>,<<"2013-11-08T18:33:07Z">>},
   {<<"lang">>,<<"en-US">>},
   {<<"results">>,
    {struct,
        [{<<"quote">>,
          [{struct,
               [{<<"symbol">>,<<"YHOO">>},
                {<<"Ask">>,<<"32.99">>},
                {<<"AverageDailyVolu"...>>,<<"18383000">>},
                {<<"Bid">>,<<"32.98">>},
                {<<"AskRealt"...>>,<<"32.9"...>>},
                {<<"BidR"...>>,<<...>>},
                {<<...>>,...},
                {...}|...]},

正如您在上面看到的,我想访问 [{struct, [{<<"symbol">>,<<"YHOO">>}]] ... 等,然后开始从那里提取数据。这就是我已经走了多远,但我似乎无法摆脱它。任何帮助表示赞赏!

Struct = mochijson2:decode(Body), Struct,
{struct, JsonData} = Struct,
{struct, Symbol} = proplists:get_value(<<"query">>, JsonData),
{struct, New} = proplists:get_value(<<"results">>, Symbol),
{struct, New1} = proplists:get_value(<<"quote">>, New),
 Id = proplists:get_value(<<"symbol">>, New1),
 Id.
4

2 回答 2

0

表达

{struct, New1} = proplists:get_value(<<"quote">>, New),

包含不正确的匹配。正确的是:

[{struct, New1}] = proplists:get_value(<<"quote">>, New),
于 2013-11-08T21:41:01.273 回答
0

为什么选择 mochijson2?试试https://github.com/talentdeficit/jsx它非常简单并且不创建原子结构所以模式匹配更容易(更少的字符),因为一旦创建了proplist。

于 2013-11-09T13:05:12.503 回答