使用JsonResult返回json数据

前端之家收集整理的这篇文章主要介绍了使用JsonResult返回json数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="js/jquery-1.11.3.js">
  4. </script>
  5. <script type="text/javascript">
  6. $(document).ready(function() {
  7. $("#myButton1").click(function() {
  8. $.post("/StudyExample/json/JSONAction.action",{},function(data,status){
  9. $("#myDiv1").html(data);
  10. });
  11. });
  12. });
  13. $(document).ready(function() {
  14. $("#myButton2").click(function() {
  15. $.post("/StudyExample/JSONAction.action",status){
  16. $("#myDiv2").html(data);
  17. });
  18. });
  19. });
  20. </script>
  21. </head>
  22. <body>
  23. <div id="myDiv1"><h2>Let AJAX change this text1</h2></div>
  24. <button id="myButton1" type="button">Change Content1</button>
  25. <div id="myDiv2"><h2>Let AJAX change this text2</h2></div>
  26. <button id="myButton2" type="button">Change Content2</button>
  27. </body>
  28. </html>
  1. import java.util.List;
  2. import java.util.Map;
  3. import java.util.HashMap;
  4. import java.util.Arrays;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import javax.servlet.http.HttpServletResponse;
  8. import net.sf.json.JSONObject;
  9. import org.apache.struts2.ServletActionContext;
  10. import org.springframework.context.annotation.Scope;
  11. import org.springframework.stereotype.Controller;
  12. import com.opensymphony.xwork2.ActionSupport;
  13.  
  14. import com.jaeson.hibernatestudy.bean.Address;
  15. import com.jaeson.hibernatestudy.bean.User;
  16.  
  17. @SuppressWarnings("all")
  18. @Scope("prototype")
  19. @Controller("jsonAction")
  20. public class JSONAction extends ActionSupport {
  21.  
  22. @Override
  23. public String execute() throws Exception {
  24. map.put("jaeson",new Address("海淀",100,"1361"));
  25. map.put("chenzq",new Address("昌平",101,"1352"));
  26. return SUCCESS;
  27. }
  28. /**
  29. * 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),* HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)的PrintWriter实例。这样就会
  30. * 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。
  31. */
  32. public void writeJSON() throws IOException{
  33. HttpServletResponse response=ServletActionContext.getResponse();
  34. response.setContentType("text/html;charset=utf-8");
  35. PrintWriter out = response.getWriter();
  36. /*String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello,i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
  37. out.println(jsonString);
  38. out.flush();
  39. out.close();*/
  40. Map<String,User> map = new HashMap<String,User>();
  41. User user1 = new User();
  42. user1.setId("1001");
  43. user1.setUserName("chenzq");
  44. user1.setAddress(new Address("beijing",100021,"134"));
  45. User user2 = new User();
  46. user2.setId("1002");
  47. user2.setUserName("jaesonchen");
  48. user2.setAddress(new Address("shanghai",200021,"133"));
  49. map.put("user1",user1);
  50. map.put("user2",user2);
  51. JSONObject json = new JSONObject();
  52. json.accumulate("map",map);
  53. //JSONObject json = JSONObject.fromObject(map);
  54. out.println(json.toString());
  55. out.flush();
  56. out.close();
  57. }
  58. private String fieldString = "String value";
  59. private int fieldInt = 100;
  60. private double fieldDouble = 99.1;
  61. private boolean fieldBoolean = false;
  62. private int[] arrayInt = {101,102,103};
  63. private List<String> list = Arrays.asList("Buenos Aires","Córdoba La","La Plata");
  64. private Map<String,Address> map = new HashMap<String,Address>();
  65. //field without getter method 不会被序列化
  66. private String fieldNoGetter = "NoGetter value";
  67. public String getFieldString() {
  68. return fieldString;
  69. }
  70. public int getFieldInt() {
  71. return fieldInt;
  72. }
  73. public double getFieldDouble() {
  74. return fieldDouble;
  75. }
  76. public boolean isFieldBoolean() {
  77. return fieldBoolean;
  78. }
  79. public int[] getArrayInt() {
  80. return arrayInt;
  81. }
  82. public List<String> getList() {
  83. return list;
  84. }
  85. public Map<String,Address> getMap() {
  86. return map;
  87. }
  88. /**
  89. public String getFieldNoGetter() {
  90. return fieldNoGetter;
  91. }*/
  92. }

不使用JsonResult的配置:

<action name="JSONAction" class="jsonAction" method="writeJSON" />

使用JsonResult的配置:

  1. <package name="json" extends="json-default" namespace="/json">
  2. <action name="JSONAction" class="jsonAction">
  3. <result type="json">
  4. <!-- 指定内容类型,默认为application/json,IE浏览器会提示下载 -->
  5. <param name="contentType">text/html</param>
  6. <!-- 默认将会序列所有有返回值的getter方法的值,而无论该方法是否有对应属性
  7. root代表从哪里开始序列化,如果root是对象则会将root对象中所有的getter方法都进行序列化。 -->
  8. <!-- <param name="root">map</param> -->
  9. <!-- 指定是否序列化空的属性,默认为false,序列化空属性,对象为null,string为"" -->
  10. <!-- <param name="excludeNullProperties">true</param> -->
  11. <!-- 这里指定将序列化map中的那些属性 -->
  12. <!-- <param name="includeProperties">userList.*</param> -->
  13. <!-- 这里指定将要从map中排除那些属性,这些排除的属性将不被序列化,一般不与上边的参数配置同时出现。
  14. 如果相同属性同时指定exclude和include,则exclude的优先级高,因此不序列化该属性-->
  15. <!-- <param name="excludeProperties">
  16. SUCCESS
  17. </param> -->
  18. </result>
  19. </action>
  20. </package>

猜你在找的Json相关文章