springmvc转换json数据
来源:互联网 发布:mac流程图制作软件 编辑:程序博客网 时间:2024/05/16 12:35
1接受JSON格式数据:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试接收JSON格式的数据</title> <%--<link rel="stylesheet" href="${pageContext.request.contextPath}/js/json2.js" />--%> <script type="text/javascript" src="../js/json2.js"></script> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ testRequestBody(); }); function testRequestBody(){ $.ajax("${pageContext.request.contextPath}/json/testRequestBody",// 发送请求的URL字符串。 { dataType : "json", // 预期服务器返回的数据类型。 type : "post", // 请求方式 POST或GET contentType:"application/json", // 发送信息至服务器时的内容编码类型 ,表示发送的内容是json类型 // 发送到服务器的数据。 data:JSON.stringify({id : 1, name : "spring",author:"jkf"}), async: true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求 // 请求成功后的回调函数。 success :function(data){ console.log(data); $("#id").html(data.id); $("#name").html(data.name); $("#author").html(data.author); }, // 请求出错时调用的函数 error:function(){ alert("数据发送失败"); } }); } </script></head><body>编号:<span id="id"></span><br>书名:<span id="name"></span><br>作者:<span id="author"></span><br></body></html>
package controller;import com.alibaba.fastjson.JSONObject;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.util.JSONPObject;import model.Book;import model.Car;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletResponse;import java.util.ArrayList;import java.util.List;import java.util.logging.Logger;@Controller@RequestMapping("/json")public class BookController { @RequestMapping("/testRequestBody") public void setJosn(@RequestBody Book book, HttpServletResponse response) throws Exception{ String s = JSONObject.toJSONString(book);//fastjson接受json数据 response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(JSONObject.toJSONString(book)); } }
2.返回JSON格式的数据
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/12/18 0018 Time: 10:40 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>测试返回Json格式的数据</title> <script type="text/javascript" src="/js/json2.js"></script> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ testResponseBody(); }); function testResponseBody(){ $.post("${packContext.request.contextPath}/json/testResponseBody",null,function(data){ //发送请求到具体的控制器,@ResponseBody返回一个json数据 $.each(data,function(){ var tr = $("<tr align='center'/>") $("<td/>").html(this.id).appendTo(tr); $("<td/>").html(this.name).appendTo(tr); $("<td/>").html(this.author).appendTo(tr); $("#booktable").append(tr); }) },"json") } </script></head><body><table id="booktable" border="1" style="border-collapse: collapse"> <tr align="center"> <th>编号</th> <th>书名</th> <th>作者</th> </tr></table></body></html>
package controller;import com.alibaba.fastjson.JSONObject;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.util.JSONPObject;import model.Book;import model.Car;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletResponse;import java.util.ArrayList;import java.util.List;import java.util.logging.Logger;@Controller@RequestMapping("/json")public class BookController { @RequestMapping("/testResponseBody") public @ResponseBody Object getJson(){ List<Book> books = new ArrayList<Book>(); books.add(new Book("1","spring+mybatis企业应用实战","肖吉文")); books.add(new Book("2","Java疯狂讲义","李刚")); return books; }}
json需要的pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.39</version> </dependency>
阅读全文
0 0
- springmvc转换json数据
- SpringMVC中json数据的转换
- springmvc .html请求返回json数据转换错误
- SpringMVC返回json数据的日期格式统一转换
- SpringMVC处理json数据
- springmvc 返回 json数据
- springmvc返回json数据
- SpringMVC处理JSON数据
- springmvc处理json数据
- SpringMVC返回json数据
- springmvc json 数据交互
- SpringMVC返回JSON数据
- SpringMVC返回JSON数据
- SpringMVC返回json数据
- springmvc:json数据交互
- SpringMVC--JSON数据
- SpringMvc处理json数据
- springmvc返回json数据
- 基于GM(0,N)的时间序列预测R实现
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- Exception: Java gateway process exited before sending the driver its port number
- xcode9 上传项目报错 iTunes Store Operation Failed Missing required icon file. The bundle does not contain
- Android Studio如何创建自己的library,供别人使用?
- springmvc转换json数据
- java中Vector的实现
- Spring Boot整合RabbitMQ实例
- 简述TCP的三次握手过程
- 关于ThreadLocal
- Markdown中如何插入EXCEL表格(转)
- mysqldump 导出数据库报错“does not exist when using LOCK T
- 使用OkHttp高效开发调试
- mybatis #和$的区别