JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面

时间:2022-10-04 07:05:32

将Set集合对象转换为JSon格式的对象字符串,返回给界面

需要导入的jar包:

JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面

编写: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 net.sf.json.JSONArray; public class JavaBeanToJOSNString extends HttpServlet { /** 第02步:重写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(getSetToJsonString());
out.flush();
out.close();
} /** 第01步:创建JSon格式字符串转换方法:将Set集合转换成json类型字符串 */
public String getSetToJsonString() {
Set set=new HashSet();
set.add(Boolean.TRUE);
set.add(new Integer());
set.add(new String[]{"zhang3","li4","wang5"});
set.add("function(arrayIndex){return this.arrayType[arrayIndex];}");
JSONArray setToJsonArray=new JSONArray().fromObject(set);
System.out
.println("com.west.webcourse.servlet.JavaBeanToJOSNString.java::"
+ setToJsonArray);
return setToJsonArray.toString();
}
}

测试servlet:

package com.west.webcourse.servlet;
/**
* 第03步:测试
* 下一步:配置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.getSetToJsonString();
} }

配置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>
<!-- 第04步:配置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="">
<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>
<!-- 第05步:编写界面 -->
动态获得当前项目的ContextPath所对应的目录:${pageContext.request.contextPath}
<br>
<a href="javaBeanToJOSNString">向servlet发送请求</a>
<br>
</body>
</html>