Json変換

7523 ワード

次に、テキスト回転json、配列回転json、listリスト回転json、setリスト回転json、mapテーブル回転json、javaBean装換jsonについて説明します.
package Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;

import Bean.StudentBean;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

/**
 * Servlet implementation class JsonServlet
 */
@WebServlet("/JsonServlet")
public class JsonServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public JsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /*response.getWriter().append("Served at: ").append(request.getContextPath());*/

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        //        JSON
        out.println("{\"id\":1,\"name\":  ,\"pwd\":1234aa}");

        //     json
        String []num = {"123","  ","ahjdgf"};
        JSONArray StrNum = JSONArray.fromObject(num);
        out.println("
"
); out.println(StrNum); //list json List strJson = new ArrayList(); strJson.add(" "); strJson.add("1"); strJson.add("fdg"); JSONArray Num = JSONArray.fromObject(strJson); out.println("
"
); out.println(Num); //set json Set setJson = new HashSet<>(); setJson.add(true); setJson.add(22); setJson.add(" "); setJson.add(34.467); JSONArray setNum = JSONArray.fromObject(setJson); out.println("
"
); out.println(setNum); //map json Map mapJson = new HashMap(); mapJson.put("id", "11"); mapJson.put("age", 22); mapJson.put("Address"," "); JSONArray MapNum = JSONArray.fromObject(mapJson); out.println("
"
); out.println(MapNum); // javaBean json StudentBean bean = new StudentBean(); bean.setId("123"); bean.setName(" "); bean.setAge(22); bean.setBirthday(new Date()); JSONArray BeanNum = JSONArray.fromObject(bean); out.println("
"
); out.println(BeanNum); //javaBean drithday JsonConfig config = new JsonConfig(); config.setExcludes(new String[]{"birthday"}); JSONArray conJson = JSONArray.fromObject(BeanNum, config); out.println("
"
); out.println(conJson); // (map ) mapJson.put("student", conJson); JSONArray Json = JSONArray.fromObject(mapJson); out.println("
"
); out.println(Json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }