2016-11-02 55 views
0

我使用HTTPoison來獲取elixir指南網站,然後使用Floki解析它以構建一個HTML 2 Jupyter Notebook變壓器(使用Markdown作爲描述)。我必須投入「反撥」。用於代碼突出顯示的\u0060,迄今爲止工作。我有一些地方使用字符串插值和其他位置Enum.join ""來處理和轉換HTML到Markdown。使用unicode編碼json使用毒物

根據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 
+0

您試圖編碼的值看起來不像地圖,而是一個元組。在此處粘貼您的原始值(和/或試圖對其進行編碼的代碼) – Sheharyar

+0

Poison不編碼元組。 –

回答

2

這裏的問題是,你正試圖編碼Tuple,而毒只能用地圖和列表的作品。如果您試圖編碼的值是一個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.\"]}"} 
+0

感謝您的提示...去重構我的代碼。其實我只使用地圖和列表,但在解析HTML樹的遞歸中我有一些flat_maps。因此,我的一些地圖'%{cell_type :: code,metadata:%{},source:[「0o777」]}被轉換爲帶有元組的關鍵字列表。 [{:cell_type,:markdown},{:metadata,%{}},{:source,[「#Basic types」]},{:cell_type,:markdown},{:metadata,%{}},{ :source,[]},' –

+0

將問題的名稱更改爲更接近代表問題所在可能很有用。這會讓其他人更容易參考。 – mmartinson