Reputation: 11
In java
HashMap<String, HashMap<String, String>> modFieldHash = new HashMap<String,HashMap<String, String>>();
JSONObject modFieldJson = new JSONObject(modFieldHash);
request.setAttribute("hash",modFieldJson);
In jsp,
JSONObject modFieldHash = (JSONObject)request.getAttribute("hash");
In javascript,
var modField = JSON.parse(<%=modFieldHash%>);
is my code. If hashmap is <"chk",<"chk1","chk1">>
, then it is received as
{"chk","{chk1=chk1}"}
in js.
JSON.parse wont work on the second hashmap.
Upvotes: 1
Views: 554
Reputation: 350725
For nested hash maps you will get better JSON
rendering with the GSON library.
Use it in java
as follows:
HashMap<String, HashMap<String, String>> modFieldHash =
new HashMap<String,HashMap<String, String>>();
Gson gson = new Gson();
request.setAttribute("hash", gson.toJson(modFieldHash));
Upvotes: 1