加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson针对map的简单测试

发布时间:2020-12-16 18:49:33 所属栏目:百科 来源:网络整理
导读:能够很好的支持复杂对象序列化和反序列化的json工具有阿里巴巴的fastjson和Google的gson,但是两个还不一样。gson参见:http://www.jb51.cc/article/p-npymqfwk-rw.html 下面是我对fastjson针对map类型的简单测试结果: package com.yjl.javabase.json;impor

能够很好的支持复杂对象序列化和反序列化的json工具有阿里巴巴的fastjson和Google的gson,但是两个还不一样。gson参见:http://www.52php.cn/article/p-npymqfwk-rw.html

下面是我对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,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}}

{{"age":11,"lastName":"bbb"}:"three",{"age":22,"lastName":"ddddd"}:"four"}
当map的key是实体类的时候出来的结果不是标准的json,而gson使用数组表示的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读