关于对象转JSON的例子

 

package com.cust.renwuyi.controller;

 

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import net.sf.json.JSONArray;

//import org.json.JSONObject;//跟下面的import net.sf.json.JSONObject; 不共存

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

import com.cust.renwuyi.pojo.Renwuyi;

 

import net.sf.json.JSONObject;

//他的使用方法    JSONObject jsonObject =JSONObject.fromObject(user);

 

@Controller

public class ReuwuyiController {

 

@RequestMapping("/json1.do")

public void user(HttpServletResponse response, HttpServletRequest request) throws IOException {

Renwuyi user = new Renwuyi("坂田银时", "", 26);

// user对象转换成JSONOBJECT

JSONObject jsonObject =JSONObject.fromObject(user);

 

// ResponseBodyJSON.out(response,jsonObject.toString());

 

// 编码类型

response.setCharacterEncoding("utf-8");

// 请求头

response.setContentType("text/html; charset=utf-8");//要加上编码类型跟请求头,中文最后能显示出来.

 

response.getWriter().print(jsonObject.toString());

}

 

@RequestMapping("/json2.do")

public void user_list(HttpServletResponse response, HttpServletRequest request) throws IOException {

List<Renwuyi> ListUser = new ArrayList<Renwuyi>(0);

ListUser.add(new Renwuyi("志村新八", "", 16));

// user list转换成JSONOBJECT

JSONArray jsonListUser =JSONArray.fromObject(ListUser);

// 编码类型

response.setCharacterEncoding("utf-8");

// 请求头

response.setContentType("text/html; charset=utf-8");

 

response.getWriter().print(jsonListUser.toString());

}

 

@RequestMapping("/json3.do")

public void user_map(HttpServletResponse response, HttpServletRequest request) throws IOException {

Map<String, Renwuyi> mapUser = new HashMap<String, Renwuyi>(0);

mapUser.put("1", new Renwuyi("神乐", "", 16));

// user  map转换成JSONOBJECT

//JSONObject jsonMapUser = new JSONObject(mapUser);

JSONArray jsonarray= JSONArray.fromObject(mapUser);

// 编码类型

response.setCharacterEncoding("utf-8");

// 请求头

response.setContentType("text/html; charset=utf-8");

 

response.getWriter().print(jsonarray.toString());

}

 

}

 

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

  <input type="button" id="submitForm1"  value="提交1"/>

<input type="button" id="submitForm2"  value="提交2"/>

<input type="button" id="submitForm3"  value="提交3"/>

<script type="text/javascript">

function a(){

 $('#submitForm1').click(function(){

   var a;

   $.ajax({

   type:"POST",

   dataType:"JSON",

   url:"/json1.do",

   success:function(data){

   alert(data);

   }

   

   });

   });

}

 

function b(){

 $('#submitForm2').click(function(){

   var a;

   $.ajax({

   type:"POST",

   dataType:"JSON",

   url:"/json2.do",

   success:function(data){

   alert(data);

   }

   

   });

   });

}

 

function c(){

 $('#submitForm3').click(function(){

   var a;

   $.ajax({

   type:"POST",

   dataType:"JSON",

   url:"/json3.do",

   success:function(data){

   alert(data);

   }

   

   });

   });

}

</script>

</body>

</html>

posted @ 2018-07-17 09:06  Demons咸鱼  阅读(3467)  评论(0编辑  收藏  举报