格式一
function userinfo(username, password){
this.username = username;
this.password = password;
}
function sendAjax1(){
var userinfoRef = new userinfo("今天很热","123");
var jsonStringRef = JSON.stringify(userinfoRef);
$.ajax({
type: "POST",
data: jsonStringRef,
url: "getJSON1.spring?t=" + new Date().getTime(),
contentType: "application/json",
});
}
@RequestMapping("getJSON1")
public void createJSON1(@RequestBody Userinfo userinfo) {
System.out.println("username value=" + userinfo.getUsername());
System.out.println("password value=" + userinfo.getPassword());
}
格式二
function sendAjax2(){
var myArray = new Array();
myArray[0] = "星期一";
myArray[1] = "星期二";
myArray[2] = "星期三";
myArray[3] = "星期四";
myArray[4] = "星期五";
var jsonString = JSON.stringify(myArray);
$.ajax({
type:"POST",
data:jsonString,
url:"getJSON2.spring?t="+new Date().getTime(),
contentType:"application/json"
});
}
@RequestMapping("getJSON2")
public void createJSON2(@RequestBody ArrayList<String> list){
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
格式三
function userinfo(username, password){
this.username = username;
this.password = password;
}
function sendAjax3(){
var myArray = new Array();
myArray[0] = new userinfo("星期一","看电影");
myArray[1] = new userinfo("星期二","逛街");
myArray[2] = new userinfo("星期三","打球");
myArray[3] = new userinfo("星期四","跑步");
myArray[4] = new userinfo("星期五","休息");
var jsonString = JSON.stringify(myArray);
$.ajax({
type:"POST",
data:jsonString,
url:"getJSON3.spring?t="+new Date().getTime(),
contentType:"application/json"
});
}
@RequestMapping("getJSON3")
public void createJSON3(@RequestBody ArrayList<Map> list){
for(int i=0;i<list.size();i++){
Map map = list.get(i);
System.out.println(map.get("username")+" "+map.get("password"));
}
}
格式四
function sendAjax4(){
var jsonObject = {
"username":"张三丰",
"kongfu":[{"taiji":"太极"},{"taiji":"太极拳"}],
"school":{"small":"小和尚","old":"掌门"}
}
var jsonString = JSON.stringify(jsonObject);
$.ajax({
type:"POST",
data:jsonString,
url:"getJSON4.spring?t="+new Date().getTime(),
contentType:"application/json"
});
}
@RequestMapping("getJSON4")
public void createJSON4(@RequestBody Map map){
System.out.println(map.get("username"));
List<Map> kongfu = (List) map.get("kongfu");
for(int i=0;i<kongfu.size();i++){
Map eachkongfu = kongfu.get(i);
System.out.println(eachkongfu.get("taiji"));
}
Map schoolmap = (Map) map.get("school");
System.out.println(schoolmap.get("small"));
System.out.println(schoolmap.get("old"));
}