我尝试使用 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 = ("{"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);