使用unicode毒药编码json

时间:2016-11-02 11:26:48

标签: elixir elixir-poison

我使用HTTPoison获取elixir指南网站,然后使用Floki解析它以构建HTML 2 Jupyter Notebook变换器(使用Markdown作为描述)。我必须加入'反击。 \u0060代码突出显示,到目前为止有效。我有一些地方使用字符串插值"#{Floki.text(childs_nodes)}"和其他地方Enum.join ""来处理和转换从HTML到Markdown。

根据jupyter notebookformat将转换后的结果存储在地图中。当我调用Poison.encode notebook时,我收到错误,因为代码点消失了。我尝试了不同的东西,但还不知道,问题出在哪里。

在处理文本时,有什么提示我做错了什么?这是例外:

** (Poison.EncodeError) unable to encode value: {:source, ["Elixir also    provides `Port`, `Reference` and `PID` as data types (usually used in process communication), and we will take a quick look at them when talking about processes. For now, let’s take a look at some of the basic operators that go with our basic types."]}
lib/poison/encoder.ex:377: Poison.Encoder.Any.encode/2
lib/poison/encoder.ex:255: anonymous fn/3 in Poison.Encoder.List.encode/3
lib/poison/encoder.ex:256: Poison.Encoder.List."-encode/3-lists^foldr/2-1-"/3
lib/poison/encoder.ex:256: Poison.Encoder.List.encode/3
lib/poison.ex:41: Poison.encode!/2
(guide2nb) lib/cli.ex:27: CLI.process/1
(elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2

1 个答案:

答案 0 :(得分:2)

问题在于您尝试对Tuple进行编码,而Poison仅适用于地图和列表。如果您尝试编码的值是地图而不是元组,它会很完美。 Unicode与此无关。

iex(1)> value = %{source: ["Elixir also    provides `Port`, `Reference` and `PID` as data types (usually used in process communication), and we will take a quick look at them when talking about processes. For now, let’s take a look at some of the basic operators that go with our basic types."]}
iex(2)> Poison.encode(value)
{:ok,
 "{\"source\":[\"Elixir also    provides `Port`, `Reference` and `PID` as data types (usually used in process communication), and we will take a quick look at them when talking about processes. For now, let’s take a look at some of the basic operators that go with our basic types.\"]}"}