fastjson针对map的简单测试

能够很好的支持复杂对象序列化和反序列化的json工具有阿里巴巴的fastjson和Google的gson,但是两个还不一样。gson参见:http://blog.csdn.net/lk_blog/article/details/7685210

下面是我对fastjson针对map类型的简单测试结果:

package com.yjl.javabase.json;


import com.alibaba.fastjson.JSON;

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

public class FastJsonTest {
	public static void main(String[] args) {
		Map<String, Student> stringStudentMap = new HashMap<>();
		Student s1 = new Student(11, "aaa", "bbb", 111.111, new Date());
		Student s2 = new Student(22, "cccc", "ddddd", 222.222, new Date());
		stringStudentMap.put("one", s1);
		stringStudentMap.put("tow", s2);
		System.out.println(JSON.toJSONString(stringStudentMap));
		System.out.println();
		Map<Student, String> studentStringMap = new HashMap<>();
		studentStringMap.put(s1, "three");
		studentStringMap.put(s2, "four");
		System.out.println(JSON.toJSONString(studentStringMap));
	}
}

class Student {
	private int age;
	private String firstName;
	private String lastName;
	private double heigth;
	private Date birth;

	public Student(int age, String firstName, String lastName, double heigth, Date birth) {
		this.age = age;
		this.firstName = firstName;
		this.lastName = lastName;
		this.heigth = heigth;
		this.birth = birth;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public double getHeigth() {
		return heigth;
	}

	public void setHeigth(double heigth) {
		this.heigth = heigth;
	}
}



结果如下:


{"one":{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"},"tow":{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}}

{{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"}:"three",{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}:"four"}

当map的key是实体类的时候出来的结果不是标准的json,而gson使用数组表示的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值