HashMap returned by JSON in JS
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- <%@ taglib prefix="s" uri="/struts-tags" %> --> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="jquery.min.js" type="text/javascript"></script> <script src="jquery-ui.min.js" type="text/javascript"></script> <link rel="stylesheet" href="jquery-ui.css" type="text/css" /> <script src="js/myJS.js" type="text/javascript"></script> </head> <body> </body> </html> |
JS
$(document).ready(function(){ initPage(); initFunctions(); }); function initPage(){ } function globalOperation(url,param,successOperationName,failOperationName){ $.ajax({ type:"POST", url:url, data:param, success:function(data){ if(data.status){ if(data.status.code==200){ clearTipMessage(); successOperationName(data); }else{ failOperationName(data); } }else{ alert("data.status has no value!Please check the action!"); } }, dataType:"json" }); } function clearTipMessage(){ $("#div_sys_msg").text(""); } function fail_global(data){ alert("data.status.msg"); $("#div_sys_msg").text(data.status.msg); } function initFunctions() { // HashMap-type data is returned. globalOperation("myHashMap.action", "", myHashMapSuc, fail_global); } function myHashMapSuc() { if (null == arguments[0].myHashMapData) { alert("There is no myHashMapData data in the database."); } var myHashMapKeysLoc = arguments[0].myHashMapData; var myHashMapKeys = Object.keys(myHashMapKeysLoc); for (var i = 0; i<myHashMapKeys.length; i++) { var keyValue = myHashMapKeys[i]; if (true == myHashMapKeysLoc.hasOwnProperty(keyValue)) { var item = myHashMapKeysLoc[keyValue]; for (var j = 0; j<item.length; j++) { var sla = item[j].sla; } } } return; } |
Action
public String myHashMap() { try { if (null == myHashMapData) { myHashMapData = new HashMap<String, List<Metrics>>(); } myHashMapData.clear(); int listNum = 1; for (int i = 97; i < 97+5; i++) { List<Metrics> data = new ArrayList<Metrics>(); for (int j = 0; j<listNum; j++) { Metrics ffData = new Metrics(new BigDecimal((double)j/listNum),new BigDecimal((double)j/listNum),new BigDecimal((double)j/listNum), new BigDecimal((double)j/listNum),new BigDecimal((double)j/listNum)); data.add(ffData); } char[] asciiChar = {(char)i}; myHashMapData.put(new String(asciiChar), data); listNum++; } status.setCode(Status.CODE_SUCCESS); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } |
json returned:
Response: { Json: |