如何从struct proplist JSON对象中提取正确的信息

时间:2013-11-08 19:39:38

标签: json parsing erlang

使用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,[{&lt;&lt;“符号”&gt;&gt;,&lt;&lt;“YHOO”&gt;&gt;}]] ...等,并开始从那里拉出数据。这是我走了多远,但我似乎无法把它弄出来。任何帮助表示赞赏!

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.

2 个答案:

答案 0 :(得分:0)

表达

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

包含不正确的匹配。正确地说:

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

答案 1 :(得分:0)

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