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

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

 

需导入的jar包:

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

 

编写servlet:

package com.west.webcourse.servlet;



import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;



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 {



    /** 第03步:重写doGet()方法,下一步:web.xml */

    @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(getMapToJsonString());

        out.flush();

        out.close();

    }



    /** 第02步:创建JSon格式字符串转换方法:将map类型转换成json类型字符串 */

    public String getMapToJsonString() {

        Map map=new HashMap();

        map.put("string", "字符串格式的数据!");

        map.put("bool",Boolean.TRUE);

        map.put("int", new Integer(1));

        map.put("array", new String[]{"zhang3","li4","wang5"});

        map.put("function", "function(arrayIndex){return this.arrayType[arrayIndex];}");

        JSONArray somesToJsonArray = JSONArray.fromObject(map);

        System.out

                .println("com.west.webcourse.servlet.JavaBeanToJOSNString.java::"

                        + somesToJsonArray);

        return somesToJsonArray.toString();

    }

}

 

配置: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="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>

      动态获得当前项目的ContextPath所对应的目录:${pageContext.request.contextPath}<br>

      <a href="javaBeanToJOSNString">向servlet发送请求</a>

      <br>

  </body>

</html>

 

你可能感兴趣的:(json)