日期:2014-05-16  浏览次数:20414 次

ProtoType json 传递集合对象

最近使用到prototype的json对象传递,下面给出一个demo例子,传递任何集合可采用类似的方法传递。

前端:

new Ajax.Request('/Action.do?p=getCountry',{
????? method: 'post',
????? requestHeaders:{Accept:'application/json'},
????? parameters:'id='+id,
????? onComplete:function(data){
???????? var json = data.responseText.evalJSON(true);?
?????????json.each(function(obj){?
????????????? $("id").value = obj.id;
????????????? $("bx").value = obj.bx;
?????????});
?????? }
??});

?

后台:

response.setContentType("text/x-json;charset=UTF-8");

JSONArray resultArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < list.size(); i++) {?
????Country c = (Country) list.get(i);

????JSONObject json = new JSONObject();
????json.put("id", c.getId());
????json.put("bx", kh_khddcjr.getBx());
????resultArray.put(json);

}

String json = resultArray.toString();

response.getWriter().print(json);
response.getWriter().flush();

return null;