ITeye博客

ProtoType json 传递集合对象

jaw111 · 2011-05-21 09:24 · Web前端 · Markdown

最近使用到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;

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。