编码 json 使用 unicode 毒药

Encoding json using poison with unicode

我正在使用 HTTPoison 获取长生不老药指南网站,然后使用 Floki 对其进行解析以构建一个 HTML 2 Jupyter Notebook 转换器(使用 Markdown 进行描述)。我必须加上`反引号。 \u0060 用于代码突出显示,目前有效。我在某些地方使用字符串插值 "#{Floki.text(childs_nodes)}",在其他地方 Enum.join "" 处理并从 HTML 转换为 Markdown。

转换后的结果按照jupyter notebook格式存储在map中。当我调用 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

这里的问题是您正在尝试对 Tuple 进行编码,而 Poison 仅适用于地图和列表。 如果您尝试编码的值是一个 Map 而不是一个元组,它会完美地工作。 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.\"]}"}