---
title: "ProtoType json 传递集合对象"
author: "jaw111"
date: "2011-05-21T09:24:37Z"
tags: []
categories: ["Web前端"]
slug: "prototype-json"
draft: false
lastmod: "2011-05-21T09:24:37Z"
id: 1052506
url: "https://caryjava.iteye.com/blog/1052506"
---

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