Struts2转换json
一、javaScript中有两种Json的语法,一种用于创建对象,另一种用于创建数组。
1.javaScript创建对象
<!-- 简单类型 --> <script type="text/javascript"> var studnt={"no":"s001","name":"ix"}; alert(studnt.no); </script> <!-- 复杂类型 --> <script type="text/javascript"> var person={ "id":"001", "name":"csdn", "son":{"no":"s001","name":"ix"}, "info":function(){document.write("姓名:"+this.name+"孩子:"+this.son.name);} }; alert(person.info()); </script>
2.javaScript创建数组
<!-- 数组类型 --> <script type="text/javascript"> var persons=[{"no":"s001","name":"i1"},{"no":"s002","name":"i2"},{"no":"s003","name":"i3"}]; alert(persons[1].name); </script>
二、json数据
1.第一种方法传回json
@Override // 第一种,直接返回jsp作用域里 public String execute() throws Exception { User user = new User(); user.setName("csdn"); user.setPass("pass"); user.setAge(20); user.setScore(100d); user.setGender(true); // 声明JSONObject对象 JSONObject jo = new JSONObject(); // 转化User为名字为User的json格式,存放于JSONObject对象 jo.accumulate("user", user); // 存放到session jo.toString()得到转换后的 ActionContext.getContext().getSession().put("user", jo.toString()); return SUCCESS; }User.java
package model; import java.util.Date; import org.apache.struts2.json.annotations.JSON; public class User { private String name; private String pass; private int age;//private transient int age;1、早期的??? private double score; private boolean gender; private Date birth; //@JSON(name="xingming")//改名字 //@JSON(serialize=false)//去掉名字//必须放在get方法之上 public String getName() { return name; } public void setName(String name) { this.name = name; } @JSON(serialize=false) public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } @JSON(format="yyyy-MM-dd") public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } }struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="hello" namespace="/hello" extends="struts-default"> <action name="login" class="action.LoginAction" > <result name="success"> /index.jsp </result> </action> </package> </struts>
index.jsp
<!-- 输出从Action传回json --> <%=request.getSession().getAttribute("user").toString() %>
2.第二种方法传回json
// 第二种,返回jsp数据到客户,外接插件或设备 public void doAction() throws Exception { User user = new User(); user.setName("csdn"); user.setPass("pass"); user.setAge(20); user.setScore(200d); user.setGender(true); // 声明JSONObject对象 JSONObject jo = new JSONObject(); // 转化User为名字为User的json格式,存放于JSONObject对象 jo.put("name","csdnn");//附加数据{"name":"csdnn","user":{"age":20,"birth":null,"gender":true,"name":"csdn","pass":"pass","score":200}} jo.accumulate("user", user); // 得到response對象 HttpServletResponse response = ServletActionContext.getResponse(); // 设置格式 response.setContentType("text/html"); PrintWriter out = response.getWriter(); // 输出到客户端 out.write(jo.toString()); out.flush(); out.close(); }
<package name="hello" namespace="/hello" extends="struts-default"> <action name="doAction" class="action.LoginAction" method="doAction"> </action> </package>
3.第三种方法传回json
package action; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import net.sf.json.JSONObject; import model.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private HashMap<String, Object> dataMap = new HashMap<String, Object>(); public HashMap<String, Object> getDataMap() { return dataMap; } public void setDataMap(HashMap<String, Object> dataMap) { this.dataMap = dataMap; } // 3.struts2方式返回json public String strsAction() { User user = new User(); user.setName("csdn"); user.setPass("pass"); user.setAge(20); user.setScore(200d); user.setGender(true); user.setBirth(new Date()); User user1 = new User(); user1.setName("csdn"); user1.setPass("pass"); user1.setAge(20); user1.setScore(200d); user1.setGender(true); user.setBirth(new Date()); List<User> list=new ArrayList<User>(); list.add(user); list.add(user1); /* * // 声明JSONObject对象 JSONObject jo = new JSONObject(); // * 转化User为名字为User的json格式,存放于JSONObject对象 jo.accumulate("user", user); */ //第二種传递集合的方法 dataMap.put("user", user); dataMap.put("user1", user1); //第一種传递集合的方法.dataMap.put("user", list); return SUCCESS; } }
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="json" extends="json-default"> <action class="action.LoginAction" name="json" method="strsAction"> <result type="json"> <param name="root">dataMap</param> </result> </action> </package> </struts>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。