简体   繁体   中英

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:

Unexpected token & in JSON at position 1

How to escape double quotes inside this string, to prevent adding "&quote;"?

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:

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

What I see in JS console:

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);

You need to disable the automatic HTML escaping when injecting encoded JSON like that. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM