erlang - 如何从 erlang JSON 获取特定值

标签 erlang

我有来自 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 matching proplists:get_value(<<"results">>,{[{<<"multicast_id">>,888},{<<"success">>,0},{<<"failure">>,1},{<<"canonical_ids">>,...},...]}, undefined) line 226 in p1_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/

相关文章:

functional-programming - 有哪些适合初学者的好的 Erlang 入门/教程?

适用于 Debian 和 Ubuntu 的 erlang 零依赖安装程序

erlang - emq mqtt 集群 : Client Session management

erlang - 在 Erlang 进程中使用映射而不是记录来保存状态有什么优势吗?

mysql - 如何在 Erlang 中安装驱动程序? (特别是 MySQL-otp 驱动程序)

docker - 在 Docker 容器/静默安装的 Ubuntu 20.04 上安装 Erlang/Elixir

c - Erlang enif_realloc_binary 内存复制

erlang - 分布式Erlang发送大消息有哪些性能特点?

erlang - 在RHEL上安装Rabbitmq-server

erlang - 无法在 Windows 中创建 Cookie 文件 RabbitMQ