json实现代码,将回调得到的值做展示
function show_etypename(){
$.getJSON("servlet/ShowEquipNameServlet?e_id="+$("#"+eid).val(),null,function call(data) {
var list = data.etype;
$.each(list, function(i, s) {
$("#"+etype).attr("value",decodeURI(s.typeName));
});
});
}
servlet中的代码实现:
<span style="white-space:pre"> </span>response.setContentType("text/json");
<span style="white-space:pre"> </span>request.setCharacterEncoding("utf-8");
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>String e_id=request.getParameter("e_id");
<span style="white-space:pre"> </span>String p_id=request.getParameter("p_id");
<span style="white-space:pre"> </span>String id;
<span style="white-space:pre"> </span>if (e_id==null) {
<span style="white-space:pre"> </span>id=p_id;
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>id=e_id;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>PrintWriter out = response.getWriter();
<span style="white-space:pre"> </span>//out.print(true);
<span style="white-space:pre"> </span> try{
<span style="white-space:pre"> </span> EquipmentServices es=new EquipmentServices();
<span style="white-space:pre"> </span> EquipRegist equip = es.findEquipById(id);
<span style="white-space:pre"> </span> EType etype=es.findTypeById(equip.getTypeId());
<span style="white-space:pre"> </span> String typename=etype.getTypeName();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> etype.setTypeName(URLEncoder.encode(typename,"UTF-8"));
<span style="white-space:pre"> </span> //stu.setMoney(23);
<span style="white-space:pre"> </span> List<EType> list = new ArrayList<EType>();
<span style="white-space:pre"> </span> list.add(etype);
<span style="white-space:pre"> </span>
//取集合
JSONArray jsonArray = JSONArray.fromObject(list);
JSONObject jsonObject = new JSONObject();
jsonObject.put("etype", jsonArray);
System.out.println(jsonObject.toString());
out.write(jsonObject.toString());
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> }catch(Exception e){
<span style="white-space:pre"> </span> e.printStackTrace();
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>out.flush();
<span style="white-space:pre"> </span>out.close();