且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Erlang Maps编码为JSON,并使用字符串进行解析?

更新时间:2023-02-23 11:07:13

是在Erlang中,字符串hello只是一个整数列表。编码Erlang的库映射到JSON将字符串解释为JSON列表,这就是为什么在输出中得到整数的列表。

The problem is that in Erlang the string "hello" is just a list of integer. The libraries that encode Erlang maps into JSON interpret strings as JSON lists, that is why you get a list of integers in the output.

为了获得JSON字符串,您需要使用Erlang二进制文件作为地图中的值:

In order to get JSON strings you need to use Erlang binaries as the values in your maps:

Food = #{<<"breakfast">> => <<"leftovers">>},
jiffy:encode(Food).
%%= <<"{ \"breakfast\" : \"leftovers\" }">>

jiffy 是一致的,所以它也会解码JSON字符串作为Erlang二进制文件,在使用 jiffy:decode / 1 时需要考虑。

jiffy is consistent so it will also decode JSON strings as Erlang binaries, which you need to take into account when using jiffy:decode/1.