java json 交互
页面代码:
function test(){
$.ajax({
type:'post',
timeout:8000,
url:'json/testJson.htm',
data:{"name":"zzd","sex":"man"},
//dataType:'text',
dataType:'json',
success:function(data){
/*var json = eval('('+data+')');
$("#jobAddress").val(json.jobAddress);
$("#jobName").val(json.jobName);
$("#jobMoney").val(json.jobMoney);
$("#name").val(json.user.name);
$("#password").val(json.user.password);
$("#bir").val(json.user.bir); */
$("#jobAddress").val(data.jobAddress);
$("#jobName").val(data.jobName);
$("#jobMoney").val(data.jobMoney);
$("#name").val(data.user.name);
$("#password").val(data.user.password);
$("#bir").val(data.user.bir);
}
});
}
二:后台代码:
@RequestMapping("/json/testJson.htm")
public void testJson(HttpServletRequest request,HttpServletResponse response) throws IOException{
Job job = new Job();
job.setJobName("softDeveloper");
job.setJobAddress("rizhao");
job.setJobMoney(8000);
User user = new User();
user.setName("mmm");
user.setBir(new Date());
user.setPassword("123456789102589");
job.setUser(user);
String a = JSONObject.fromObject(job).toString();
response.getWriter().write(a);
response.getWriter().flush();
/*jsonObj.put("like", "basketball");
jsonObj.put("age", 28);
String b = jsonObj.toString();*/
System.out.println(a);
}