fastjson开发学习(一)---把对象封装成json。

前端之家收集整理的这篇文章主要介绍了fastjson开发学习(一)---把对象封装成json。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先需要下载fastjson的jar包,官方也提供了下载地址和源码。

fastjson: http://www.oschina.net/p/fastjson

fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。

主要特点:

  • 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson
  • 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
  • 零依赖(没有依赖其它任何类库除了JDK)
然后创建一个Web工程,并且创建一个servlet类【FirstServlet.java】

代码如下:

  1.  

  1. package com.fastjson.test;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.alibaba.fastjson.JSON;
  12.  
  13. public class FirstServlet extends HttpServlet {
  14.  
  15. /**
  16. * Constructor of the object.
  17. */
  18. public FirstServlet() {
  19. super();
  20. }
  21.  
  22. /**
  23. * Destruction of the servlet. <br>
  24. */
  25. public void destroy() {
  26. super.destroy(); // Just puts "destroy" string in log
  27. // Put your code here
  28. }
  29.  
  30. /**
  31. * The doGet method of the servlet. <br>
  32. *
  33. * This method is called when a form has its tag value method equals to get.
  34. *
  35. * @param request the request send by the client to the server
  36. * @param response the response send by the server to the client
  37. * @throws ServletException if an error occurred
  38. * @throws IOException if an error occurred
  39. */
  40. public void doGet(HttpServletRequest request,HttpServletResponse response)
  41. throws ServletException,IOException {
  42. this.doPost(request,response);
  43. //
  44. // response.setContentType("text/html");
  45. // PrintWriter out = response.getWriter();
  46. // out
  47. // .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  48. // out.println("<HTML>");
  49. // out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  50. // out.println(" <BODY>");
  51. // out.print(" This is ");
  52. // out.print(this.getClass());
  53. // out.println(",using the GET method");
  54. // out.println(" </BODY>");
  55. // out.println("</HTML>");
  56. // out.flush();
  57. // out.close();
  58. }
  59.  
  60. /**
  61. * The doPost method of the servlet. <br>
  62. *
  63. * This method is called when a form has its tag value method equals to post.
  64. *
  65. * @param request the request send by the client to the server
  66. * @param response the response send by the server to the client
  67. * @throws ServletException if an error occurred
  68. * @throws IOException if an error occurred
  69. */
  70. public void doPost(HttpServletRequest request,IOException {
  71. response.setContentType("text/html");
  72. PrintWriter out = response.getWriter();
  73. Group group = new Group();
  74. group.setId(0L);
  75. group.setName("admin");
  76.  
  77. User guestUser = new User();
  78. guestUser.setId(2L);
  79. guestUser.setName("guest");
  80.  
  81. User rootUser = new User();
  82. rootUser.setId(3L);
  83. rootUser.setName("root");
  84.  
  85. group.getUsers().add(guestUser);
  86. group.getUsers().add(rootUser);
  87. String jsonString = JSON.toJSONString(group);
  88. System.out.println("调用后台");
  89. out.println(jsonString);
  90. out.flush();
  91. out.close();
  92. }
  93.  
  94. /**
  95. * Initialization of the servlet. <br>
  96. *
  97. * @throws ServletException if an error occurs
  98. */
  99. public void init() throws ServletException {
  100. // Put your code here
  101. }
  102.  
  103. }


然后配置【web.xml】

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. <servlet>
  8. <description>This is the description of my J2EE component</description>
  9. <display-name>This is the display name of my J2EE component</display-name>
  10. <servlet-name>FirstServlet</servlet-name>
  11. <servlet-class>com.fastjson.test.FirstServlet</servlet-class>
  12. </servlet>
  13.  
  14. <servlet-mapping>
  15. <servlet-name>FirstServlet</servlet-name>
  16. <url-pattern>/servlet/first</url-pattern>
  17. </servlet-mapping>
  18. <welcome-file-list>
  19. <welcome-file>index.jsp</welcome-file>
  20. </welcome-file-list>
  21. </web-app>


jsp页面上(整理也可以用Html页面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'json.jsp' starting page</title>
  12. <@R_502_338@ http-equiv="pragma" content="no-cache">
  13. <@R_502_338@ http-equiv="cache-control" content="no-cache">
  14. <@R_502_338@ http-equiv="expires" content="0">
  15. <@R_502_338@ http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <@R_502_338@ http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
  21. <script type="text/javascript">
  22. $(function(){
  23. $('#mylink').click(function(){
  24. alert("ok");
  25. $.get("http://192.168.0.101:8080/Test/servlet/first",function(data){
  26. alert(data);
  27. $('#result').text(data);
  28. return;
  29. });
  30. });
  31. });
  32. </script>
  33. </head>
  34. <body>
  35. <a href="javascript:void(0)" id="mylink">链接</a>
  36. <input type="button" id="mybtn" value="提交">
  37. <div id="result">
  38.  
  39. </div>
  40.  
  41. </body>
  42. </html>

猜你在找的Json相关文章