fastJson

一、简介

    Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。

二、特点

    1、高性能(速度最快

    2、功能强大

    3、无依赖

    4、开源(Apache License 2.0

三、实例

    1、demo

       

package com.cxhd.nmsp.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * fastJson实例
 * 
 * @author liyulin
 * @version 1.0 2015年8月26日 下午2:13:48
 */
public class FastJsonDemo {
	public static void main(String[] args) {
		parseObject();
		parseMap();
		parseList();
		
		fromObject();
		fromMap();
		fromList();
	}
	
	/**
	 * 字符串转Object对象
	 */
	public static void parseObject(){
		String jsonObject = "{\"username\":\"admin\",\"password\":\"123456\",\"loginDate\":\"2015-08-26 10:22:23\"}";
		Login login = JSON.parseObject(jsonObject, Login.class);
		
		System.out.println("字符串转对象(Object)==>"+login.getUsername()+":"+login.getPassword()+"\t"+login.getLoginDate());
	}
	
	/**
	 * 字符串转Map对象
	 */
	public static void parseMap(){
		String jsonObject = "{\"username\":\"admin\",\"password\":\"123456\",\"loginDate\":\"2015-08-26 10:22:23\"}";
		Map login = JSON.parseObject(jsonObject, new TypeReference>() {});
		
		System.out.println("字符串转对象(Map)==>"+login.get("username")+":"+login.get("password")+"\t"+login.get("loginDate"));
	}
	
	/**
	 * 字符串转List对象 
	 */
	public static void parseList(){
		StringBuilder jsonList = new StringBuilder();
		jsonList.append("[");
		jsonList.append("{\"username\":\"admin\",\"password\":\"123456\",\"loginDate\":\"2015-08-26 10:22:23\"},");
		jsonList.append("{\"username\":\"admin\",\"password\":\"123456\",\"loginDate\":\"2015-08-26 10:22:23\"}");
		jsonList.append("]");
		List logins = JSON.parseArray(jsonList.toString(), Login.class);
		
		System.out.println("\n字符串转对象(List)==>");
		for(Login login:logins){
			System.out.println(login.getUsername()+":"+login.getPassword()+"\t"+login.getLoginDate());
		}
	}
	
	/**
	 * Object对象转字符串
	 */
	public static void fromObject(){
		Login login = new Login();
		login.setUsername("admin");
		login.setPassword("123456");
		login.setLoginDate(new Date());
		
		String json = JSON.toJSONStringWithDateFormat(login, "yyyy-MM-dd hh:mm:ss", SerializerFeature.WriteDateUseDateFormat);
		System.out.println("\n对象(Object)转字符串==>"+json);
	}
	
	
	/**
	 * Map对象转字符串
	 */
	public static void fromMap(){
		Map map = new HashMap();
		map.put("username", "admin");
		map.put("password", "123456");
		map.put("loginDate", new Date());
		
		String json = JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd hh:mm:ss", SerializerFeature.WriteDateUseDateFormat);
		System.out.println("\n对象(Map)转字符串==>"+json);
	}
	
	/**
	 * List对象转字符串
	 */
	public static void fromList(){
		List logins = new ArrayList();
		for(int i=0; i<4; i++){
			Login login = new Login();
			login.setUsername("admin");
			login.setPassword("123456");
			login.setLoginDate(new Date());
			
			logins.add(login);
		}
		String json = JSON.toJSONStringWithDateFormat(logins, "yyyy-MM-dd hh:mm:ss", SerializerFeature.WriteDateUseDateFormat);
		System.out.println("\n对象(List)转字符串==>"+json);
	}
}

    2、Login.java

       

public class Login{
	private String username;
	private String password;
	private Date loginDate;
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}
}

你可能感兴趣的:(web)