Ajax示例
1:页面JS
var url = "./user.action?id=xxx&mdmd=xxx";
$.get(url,{
"action1":"",
"value":obj.value,
"tmp":+Math.random()
},function(data){
var v_data = jQuery.parseJSON(data);
v_List = v_data;
for(var v_i=0; v_i < v_data.length; v_i++){
var o = document.createElement("option");
document.saveForm.userId.options.add(o);
o.value=v_data[v_i].id;
o.innerHTML=v_data[v_i].name;
if(document.saveForm._userId.value == v_data[v_i].id){
o.selected = "true";
changeUserType(document.saveForm.userId);
}
}
});
2:java
import net.sf.json.JSONArray ;
List<User> userList = new ArrayList<User> ();
String jsonStr = JSONArray.fromObject(userList.toArray(new User[userList.size()])).toString();
response.setContentType("text/plain;charset=UTF-8");
try {
response.getWriter().write(jsonStr);
} catch (Exception e) {
e.printStackTrace();
}
3:示例2
json-1.7.1.jar
import com.google.gson.Gson;
String objectStr = request.getParameter("Str");
Gson gson = new Gson();
if(null == objectStr || "".equals(objectStr)){
objectStr = "";
}
String objectStr2 = URLDecoder.decode(objectStr,"UTF-8");
//从PAD发过来的字符串传成VO list
AbcVo abcVo = gson.fromJson(objectStr2, AbcVo.class);
Gson gson = new Gson();
//把vo列表对象改成string发到PAD
String ObjectStr = gson.toJson(o);
AbcVo.java
//VO对象
public class AbcVo {
private User u;
private List<dept> deptList;
private Long userId;
private String deptName;
//getters setters
}

浙公网安备 33010602011771号