[英]Unexpected token & in JSON when parsing object from string
I tried to parse data from elixir map converted to JSON using Poison.encode!(), in any case I got error: 我试图使用Poison.encode!()解析从转换为JSON的长生不老药地图中的数据,无论如何我都出错了:
Unexpected token & in JSON at position 1
How to escape double quotes inside this string, to prevent adding ""e;"? 如何在此字符串内转义双引号,以防止添加“"”?
Initial data: 初始数据:
%{"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"]}
Mapped to new structure, using this method inside controller: 使用控制器内部的此方法映射到新结构:
defp map_shards(nodes, :nodes), do: %{"nodes": Enum.map(nodes, fn{k, v} -> %{"node": k, "shards": v} end)}
Call inside index action: 调用内部索引动作:
nodes = map_shards(resp["by_node"], :nodes)
Now I have map as this: 现在我有这样的地图:
%{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 code, with parser call inside template .eex: JS代码,在模板.eex中具有解析器调用:
var theData = ("<%= Poison.Encoder.encode(@nodes, []) %>");
theData = JSON.parse(theData);
What I see in JS console: 我在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); theData = JSON.parse(theData);
You need to disable the automatic HTML escaping when injecting encoded JSON like that. 当注入这样的编码JSON时,您需要禁用自动HTML转义。 You also don't need the surrounding
"
or brackets. 您也不需要周围的
"
或方括号。
var theData = <%= raw Poison.Encoder.encode(@nodes, []) %>;
There's no need to call JSON.parse
anymore, theData
will be set to the value itself, not a string containing a JSON. 不再需要调用
JSON.parse
, theData
将被设置为值本身,而不是包含JSON的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.