将po(bean)对象转换为JSon格式的对象字符串,返回给界面
导入jar包:
编写po(bean)类:
package com.west.webcourse.po; /** * 第01步:编写bean类, * 下一步com.west.webcourse.servlet.JavaBeanToJOSNString.java */ public class PersonInfoPo { private String name; private int age; private String sex; public PersonInfoPo(){} public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
编写:servlet
package com.west.webcourse.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.west.webcourse.po.PersonInfoPo; import net.sf.json.JSONArray; public class JavaBeanToJOSNString extends HttpServlet { PersonInfoPo pif = new PersonInfoPo(); /** 第03步:重写doGet()方法,下一步:测试 */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); System.out.println("转换"); out.print("转换后的JSON字符串:"); /** 向浏览器发送JSon格式的字符串 */ out.print(getBeanToJsonString()); out.flush(); out.close(); } /** 第02步:创建JSon格式字符串转换方法:将Set集合转换成json类型字符串 */ public String getBeanToJsonString() { pif.setAge(1); pif.setName("name"); pif.setSex("sex"); JSONArray beanToJsonArray = new JSONArray().fromObject(pif); System.out.println(beanToJsonArray); return beanToJsonArray.toString(); } }
测试servlet:
package com.west.webcourse.servlet; /** * 第04步:测试 * 下一步:配置web.xml */ import org.junit.BeforeClass; import org.junit.Test; public class JavaBeanToJOSNStringTest { static JavaBeanToJOSNString js; @BeforeClass public static void setUpBeforeClass() throws Exception { js=new JavaBeanToJOSNString(); } @Test public void testToJsonString() { js.getBeanToJsonString(); } }
配置web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- 第05步:配置servlet,下一步:personinfo.jsp --> <servlet> <description>This</description> <display-name>This</display-name> <servlet-name>JavaBeanToJOSNString</servlet-name> <servlet-class>com.west.webcourse.servlet.JavaBeanToJOSNString</servlet-class> </servlet> <servlet-mapping> <servlet-name>JavaBeanToJOSNString</servlet-name> <url-pattern>/javaBeanToJOSNString</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>personinfo.jsp</welcome-file> </welcome-file-list> </web-app>
编写界面:personinfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘personinfo.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- 第06步:编写界面 --> 动态获得当前项目的ContextPath所对应的目录:${pageContext.request.contextPath} <br> <a href="javaBeanToJOSNString">向servlet发送请求</a> <br> </body> </html>
JSon_零基础_005_将po(bean)对象转换为JSon格式的对象字符串,返回给界面
原文:http://www.cnblogs.com/zjsy/p/4381463.html