我有来自 GCM 的关注
{[
{<<"multicast_id">>,888},
{<<"success">>,0},
{<<"failure">>,1},
{<<"canonical_ids">>,0},
{<<"results">>,
[
{
[
{<<"error">>,<<"NotRegistered">>}
]
}
]
}
]}
我正在尝试阅读“NotRegistered”。但我找不到阅读它的方法。
如何阅读“NotRegistered”?
我试过了
result_from(Json) ->
{
proplists:get_value(<<"multicast_id">>, Json),
proplists:get_value(<<"success">>, Json),
proplists:get_value(<<"failure">>, Json),
proplists:get_value(<<"canonical_ids">>, Json),
proplists:get_value(<<"results">>, Json)
}.
但我遇到了异常(exception)
CRASH REPORT Process
''
with 0 neighbours exited with reason: no function clause matchingproplists:get_value(<<"results">>,{[{<<"multicast_id">>,888},{<<"success">>,0},{<<"failure">>,1},{<<"canonical_ids">>,...},...]}, undefined)
line 226 inp1_server:terminate/7
line 874
最佳答案
首先,这不是 JSON,而是大多数 JSON 库用作与 JSON 相互转换的最佳结构的数据结构。正式这是一个 tuple()包含一个元素:一个列表。
您会收到此错误,因为 get_value 函数没有用于您的参数的子句,这意味着其中一个参数可能格式错误。该错误是正确的,因为 Json 参数的类型错误。
Json 变量不是 proplist。将结构转换为(prop)列表,如下所示:
{List} = Json,
然后使用它作为 proplists:get_value 的第二个参数。
第二个参数必须是 properties 的列表IE。类型为 [property()]
,但您提供的是 {[property()]}
。
关于erlang - 如何从 erlang JSON 获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37650916/