[英]How to read Map Data from JSON
我正在使用Spring 3和JQuery。
我想從我的Spring Controller返回Map {由elementID組成,其值}
並使用數據更新視圖。
控制器代碼
@RequestMapping(value="/hpcCalResult")
public ResponseEntity<Map<String, String>> calculateHeathPlanCost(HttpServletRequest request) {
Map<String,String> requestMap = getMapFromRequest(request);
boolean isError = false;
Map<String,String> responseMap = new HashMap<String, String>();
try{
responseMap = this.healthPlanService.getResponseMapAfterHPCostCalc(requestMap);
}catch(HCException e){
isError = true;
responseMap.put("error", Springi18nUtils.getMessage(e.getMessageCode().getName(), null));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if(isError){
return new ResponseEntity<Map<String, String>>(responseMap, headers, HttpStatus.INTERNAL_SERVER_ERROR);
}else
return new ResponseEntity<Map<String, String>>(responseMap, headers, HttpStatus.OK);
}
現在,我想在JSP文件ajax請求中使用此響應Map來更新ui。
我的JQUERY代碼是:
$("#Continue").click(function () {
var form = $("#calculator");
var data = form.serialize();
$.post(form.attr("action"), data ,
function( data ) {
<<< dont know how to read the data as so that i can get key values which i will use to update the form fields . >>>
});
}
);
});
請幫我解決同樣的問題。 謝謝。
嘗試理解代碼中的細節有些繁重...因此,我不確定我的回答是否會對您有所幫助。 但是,您是否嘗試在javascript中使用eval()函數? 這樣解碼json很容易。 像這樣的東西:
var themap = new Object();
themap = eval( jsonedInstances );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.