0

我尝试使用 Poison.encode!() 解析从 Elixir 映射转换为 JSON 的数据,但无论如何我都遇到了错误:

Unexpected token & in JSON at position 1

如何在此字符串中转义双引号,以防止添加“”e;”?

初始数据:

%{"node_10_10@172.17.0.11" => ["40000000-5fffffff", "a0000000-bfffffff"],
  "node_1_10@172.17.0.2" => ["20000000-3fffffff", "a0000000-bfffffff"],
  "node_2_10@172.17.0.3" => ["20000000-3fffffff", "80000000-9fffffff"],
  "node_3_10@172.17.0.4" => ["20000000-3fffffff", "80000000-9fffffff",
   "e0000000-ffffffff"],
  "node_4_10@172.17.0.5" => ["00000000-1fffffff", "80000000-9fffffff",
   "e0000000-ffffffff"],
  "node_5_10@172.17.0.6" => ["00000000-1fffffff", "60000000-7fffffff",
   "e0000000-ffffffff"],
  "node_6_10@172.17.0.7" => ["00000000-1fffffff", "60000000-7fffffff",
   "c0000000-dfffffff"],
  "node_7_10@172.17.0.8" => ["60000000-7fffffff", "c0000000-dfffffff"],
  "node_8_10@172.17.0.9" => ["40000000-5fffffff", "c0000000-dfffffff"],
  "node_9_10@172.17.0.10" => ["40000000-5fffffff", "a0000000-bfffffff"]}

映射到新结构,在控制器中使用此方法:

defp map_shards(nodes, :nodes), do: %{"nodes": Enum.map(nodes, fn{k, v} -> %{"node": k, "shards": v} end)}    

调用内部索引操作:

nodes = map_shards(resp["by_node"], :nodes)

现在我有这样的地图:

%{nodes: [%{node: "node_10_10@172.17.0.11", shards: ["40000000-5fffffff", "a0000000-bfffffff"]}, %{node: "node_1_10@172.17.0.2", shards: ["20000000-3fffffff", "a0000000-bfffffff"]}, %{node: "node_2_10@172.17.0.3", shards: ["20000000-3fffffff", "80000000-9fffffff"]}, %{node: "node_3_10@172.17.0.4", shards: ["20000000-3fffffff", "80000000-9fffffff", "e0000000-ffffffff"]}, %{node: "node_4_10@172.17.0.5", shards: ["00000000-1fffffff", "80000000-9fffffff", "e0000000-ffffffff"]}, %{node: "node_5_10@172.17.0.6", shards: ["00000000-1fffffff", "60000000-7fffffff", "e0000000-ffffffff"]}, %{node: "node_6_10@172.17.0.7", shards: ["00000000-1fffffff", "60000000-7fffffff", "c0000000-dfffffff"]}, %{node: "node_7_10@172.17.0.8", shards: ["60000000-7fffffff", "c0000000-dfffffff"]}, %{node: "node_8_10@172.17.0.9", shards: ["40000000-5fffffff", "c0000000-dfffffff"]}, %{node: "node_9_10@172.17.0.10", shards: ["40000000-5fffffff", "a0000000-bfffffff"]}]}

JS 代码,在模板 .eex 中使用解析器调用:

var theData = ("<%= Poison.Encoder.encode(@nodes, []) %>");
theData = JSON.parse(theData);

我在 JS 控制台中看到的内容:

var theData = ("{&quot;nodes&quot;:[{&quot;shards&quot;:[&quot;40000000-5fffffff&quot;,&quot;a0000000-bfffffff&quot;],&quot;node&quot;:&quot;node_10_10@172.17.0.11&quot;},{&quot;shards&quot;:[&quot;20000000-3fffffff&quot;,&quot;a0000000-bfffffff&quot;],&quot;node&quot;:&quot;node_1_10@172.17.0.2&quot;},{&quot;shards&quot;:[&quot;20000000-3fffffff&quot;,&quot;80000000-9fffffff&quot;],&quot;node&quot;:&quot;node_2_10@172.17.0.3&quot;},{&quot;shards&quot;:[&quot;20000000-3fffffff&quot;,&quot;80000000-9fffffff&quot;,&quot;e0000000-ffffffff&quot;],&quot;node&quot;:&quot;node_3_10@172.17.0.4&quot;},{&quot;shards&quot;:[&quot;00000000-1fffffff&quot;,&quot;80000000-9fffffff&quot;,&quot;e0000000-ffffffff&quot;],&quot;node&quot;:&quot;node_4_10@172.17.0.5&quot;},{&quot;shards&quot;:[&quot;00000000-1fffffff&quot;,&quot;60000000-7fffffff&quot;,&quot;e0000000-ffffffff&quot;],&quot;node&quot;:&quot;node_5_10@172.17.0.6&quot;},{&quot;shards&quot;:[&quot;00000000-1fffffff&quot;,&quot;60000000-7fffffff&quot;,&quot;c0000000-dfffffff&quot;],&quot;node&quot;:&quot;node_6_10@172.17.0.7&quot;},{&quot;shards&quot;:[&quot;60000000-7fffffff&quot;,&quot;c0000000-dfffffff&quot;],&quot;node&quot;:&quot;node_7_10@172.17.0.8&quot;},{&quot;shards&quot;:[&quot;40000000-5fffffff&quot;,&quot;c0000000-dfffffff&quot;],&quot;node&quot;:&quot;node_8_10@172.17.0.9&quot;},{&quot;shards&quot;:[&quot;40000000-5fffffff&quot;,&quot;a0000000-bfffffff&quot;],&quot;node&quot;:&quot;node_9_10@172.17.0.10&quot;}]}");

theData = JSON.parse(theData);

4

1 回答 1

3

在注入这样的编码 JSON 时,您需要禁用自动 HTML 转义。您也不需要周围"或括号。

var theData = <%= raw Poison.Encoder.encode(@nodes, []) %>;

不再需要调用JSON.parsetheData将设置为值本身,而不是包含 JSON 的字符串。

于 2017-06-07T08:46:05.570 回答