fastjson转换器简介及基本使用

0.简介


FastJson的介绍:
	Fastjson是一个Java库,可以将Java对象转换为JSON格式,当然它也可以将JSON字符串转换为Java对象。
	Fastjson可以操作任何Java对象,即使是一些预先存在的没有源码的对象。
	
FastJson的特点:
	1.FastJson数度快,无论序列化和反序列化,都是当之无愧的fast 
	2.功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum) 
	3.零依赖(没有依赖其它任何类库)

FastJson的简单说明:
	FastJson对于json格式字符串的解析主要用到了下面三个类: 
		1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换 
		2.JSONObject:fastJson提供的json对象 
		3.JSONArray:fastJson提供json数组对象

1.引入依赖(Spring相关略)

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.28</version>
</dependency>

2.Spring-mvc.xml配置

<!-- 扫描包 -->
<context:component-scan base-package="com.ronybo.controller" />
<!--配置Spring注解驱动 -->
<mvc:annotation-driven>
	<mvc:message-converters register-defaults="true">
		<!-- 配置使用阿里巴巴的JSON转换器FASTJSON -->
		<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>text/html;charset=UTF-8</value>
					<value>application/json;charset=UTF-8</value>
				</list>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	
   	<!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
	
  	<servlet>
  		<servlet-name>springmvc</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
  		<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:spring/springmvc.xml</param-value>
  		</init-param>
  		<load-on-startup>1</load-on-startup>
  	</servlet>
  
  	<servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<url-pattern>*.do</url-pattern>
 	</servlet-mapping>
</web-app>

4.编写Controller

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

import com.alibaba.fastjson.JSON;
import com.ronybo.vo.User;

@RestController
@RequestMapping("/test")
public class TestController {
	@RequestMapping("/testFastJson")
	public User getUser(String userName, String password) {
		User user = new User(111, userName, password);
		System.out.println(JSON.toJSON(user));
		return user;
	}
}

4.编写index.html

<!DOCTYPE html>
<html>
    <head>
        <title>index.html</title>
        <script type="text/javascript" src="js/jquery/jquery.min.js" charset="UTF-8"></script>
    </head>
  
    <body>
        <script type="text/javascript">
        	$.ajax({
        		url: "/fastjsondemo/test/testFastJson.do",
        		data: {"userName":"小明","password":"123456"},
        		type: "post",
        		success : function(response) {
        			// 此时是一个JSON字符串,需要转为JSON对象
        			console.log(JSON.parse(response));
        		}
        	})
        </script>
    </body>
</html>

5.控制台

酱紫

6.浏览器

传到后台的数据:
asd

后台响应的数据: 此时是一个JSON对象。

asd
字符串

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值