意想不到的令牌&在从字符串解析对象时使用JSON

时间:2017-06-07 08:42:58

标签: javascript json elixir phoenix-framework elixir-poison

我尝试使用Poison.encode!()从elixir map转换为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);

1 个答案:

答案 0 :(得分:2)

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

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

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