ID:35567247
大?。?22.26 KB
頁數(shù):9頁
時間:2019-03-29
4、;privateStringname;privateStringaddess;JsonAction.javaublicclassJsonActionextendsActionSupport{privateStringname;//接收前臺傳到action的json的字符串privateStringresult;//需要返回的json格式的字符串publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetResult()
5、{returnresult;}publicvoidsetResult(Stringresult){this.result=result;}publicStringexecute()throwsException{System.out.println("前臺所傳的json格式的字符串:"+this.name);//一、將json格式的字符串this.name轉(zhuǎn)換成Person類Personperson=(Person)JsonUtil.getObject4JsonString(this.name,Person.class);//測試是否已經(jīng)轉(zhuǎn)換Sy
6、stem.out.println("轉(zhuǎn)換成Person對象并打印出姓名:"+person.getName());//二、將Person對象轉(zhuǎn)換為json格式的字符串傳到前臺頁面//1、new一個Person類person2,并添加屬性Personperson2=newPerson();person2.setId(2);person2.setName("小紅");person2.setAddess("***市***區(qū)");//將person2轉(zhuǎn)換成json格式的字符串并賦值給this.resultthis.result=JsonUtil.getJs
7、onString4JavaPOJO(person2);//測試是否已經(jīng)轉(zhuǎn)換System.out.println("person2轉(zhuǎn)換成的json格式的字符串:"+this.result);return"success";}啟動tomcat之后的效果前臺頁面控制臺2、傳list集合到后臺functionabc(){person=[{id:1,name:"張三",addess:"武漢市武昌區(qū)"},{id:2,name:"小明",addess:"A市B區(qū)"},]$.ajax({type:"POST",url:"select.action",data:"
8、name="+JSON.stringify(person),//stringify(),json.js文件所提供的將json對象轉(zhuǎn)換成j
此文檔下載收益歸作者所有