我尝试使用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 = ("{"nodes":[{"shards":["40000000-5fffffff","a0000000-bfffffff"],"node":"node_10_10@172.17.0.11"},{"shards":["20000000-3fffffff","a0000000-bfffffff"],"node":"node_1_10@172.17.0.2"},{"shards":["20000000-3fffffff","80000000-9fffffff"],"node":"node_2_10@172.17.0.3"},{"shards":["20000000-3fffffff","80000000-9fffffff","e0000000-ffffffff"],"node":"node_3_10@172.17.0.4"},{"shards":["00000000-1fffffff","80000000-9fffffff","e0000000-ffffffff"],"node":"node_4_10@172.17.0.5"},{"shards":["00000000-1fffffff","60000000-7fffffff","e0000000-ffffffff"],"node":"node_5_10@172.17.0.6"},{"shards":["00000000-1fffffff","60000000-7fffffff","c0000000-dfffffff"],"node":"node_6_10@172.17.0.7"},{"shards":["60000000-7fffffff","c0000000-dfffffff"],"node":"node_7_10@172.17.0.8"},{"shards":["40000000-5fffffff","c0000000-dfffffff"],"node":"node_8_10@172.17.0.9"},{"shards":["40000000-5fffffff","a0000000-bfffffff"],"node":"node_9_10@172.17.0.10"}]}");
theData = JSON.parse(theData);
答案 0 :(得分:2)
在注入像这样的编码JSON时,您需要禁用自动HTML转义。您也不需要周围的"
或括号。
var theData = <%= raw Poison.Encoder.encode(@nodes, []) %>;
无需再调用JSON.parse
,theData
将设置为值本身,而不是包含JSON的字符串。