在Erlang中用mochijson2解码JSON [英] Decode JSON with mochijson2 in Erlang
本文介绍了在Erlang中用mochijson2解码JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有一些JSON数据的var:
I have a var that has some JSON data:
A = <<"{\"job\": {\"id\": \"1\"}}">>.
使用mochijson2,我解码数据:
Using mochijson2, I decode the data:
Struct = mochijson2:decode(A).
现在我有:
{struct,[{<<"job">>,{struct,[{<<"id">>,<<"1">>}]}}]}
我正在尝试阅读(例如)工作或id。
I am trying to read (for example), "job" or "id".
我尝试使用struct.get_value,但似乎不起作用。
I tried using struct.get_value but it doesn't seem to work.
任何想法?
推荐答案
数据是{struct,proplist()}格式,所以这里是你做的: / p>
The data is in {struct, proplist()} format, so here's what you do:
{struct, JsonData} = Struct,
{struct, Job} = proplists:get_value(<<"job">>, JsonData),
Id = proplists:get_value(<<"id">>, Job),
您可以在以下位置阅读更多关于proplists的信息: http://www.erlang.org/doc/man/proplists.html
You can read more about proplists at: http://www.erlang.org/doc/man/proplists.html
这篇关于在Erlang中用mochijson2解码JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文