关于对象转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>

转载于:https://www.cnblogs.com/Demons666/p/9321522.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值