欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastJson如何序列化一个包含list对象的Map对象,如Map<String,List<T>>类型的对象

程序员文章站 2022-04-03 08:54:10
...

利用阿里的json工具还是挺方便的,直接看代码


1、先有一个实体类StudentInfoDto


package com.myapp.dto;


import java.util.Date;


import com.alibaba.fastjson.annotation.JSONField;


public class StudentInfoDto extends Object{
    private Integer studentSn;


    private String studentName;
    
    private String sex;
    
    /**
     * 序列化日期,使之按照指定格式输出,如果不指定,则会输出当前时间戳
     * 比如:2017-08-02 16:50:29.929
     */
    @JSONField(format="yyyy-MM-dd HH:mm:ss.SSS")
    private Date birthday;
    
    public StudentInfoDto(){
    
    }
  public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
    
public Date getBirthday() {
    return birthday;
}
    
    public StudentInfoDto(Integer studentSn,String studentName,String sex,Date birthday){
        super();
        this.studentSn = studentSn;
        this.studentName = studentName;
        this.sex = sex;
        this.birthday = birthday;
    }


public void setSex(String sex) {
    this.sex = sex;
}
public String getSex() {
    return sex;
}
    
public Integer getStudentSn() {
    return studentSn;
}


public void setStudentSn(Integer studentSn) {
    this.studentSn = studentSn;
}


public String getStudentName() {
    return studentName;
}


public void setStudentName(String studentName) {
    this.studentName = studentName;
}
    
}

2、再写一个main类


package com.myapp.core;


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.myapp.dto.StudentInfoDto;
/**
 * 阿里json的学习
 * @author Administrator
 *
 */
public class TestMapToJson {

public static Map<String, List<StudentInfoDto>> getMapListData(){
List<StudentInfoDto> list1 = new ArrayList<StudentInfoDto>();
list1.add(new StudentInfoDto(1, "张三", "男1",new Date()));
list1.add(new StudentInfoDto(2, "李四", "女2",new Date()));
list1.add(new StudentInfoDto(3, "王五", "女3",new Date()));
List<StudentInfoDto> list2 = new ArrayList<StudentInfoDto>();
list2.add(new StudentInfoDto(7, "张七", "男7",new Date()));
list2.add(new StudentInfoDto(8, "陈八", "男8",new Date()));
list2.add(new StudentInfoDto(9, "刘九", "女9",new Date()));
Map<String, List<StudentInfoDto>> map = new HashMap<String,List<StudentInfoDto>>();
map.put("A123", list1);
map.put("B789", list2);
return map;
}

public static void main(String[] args) {
String jsString = JSON.toJSONString(getMapListData());
System.out.println(jsString);
}
}


3、控制台打印

{
    "B789":[
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"男7",
            "studentName":"张七",
            "studentSn":7
        },
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"男8",
            "studentName":"陈八",
            "studentSn":8
        },
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"女9",
            "studentName":"刘九",
            "studentSn":9
        }
    ],
    "A123":[
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"男1",
            "studentName":"张三",
            "studentSn":1
        },
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"女2",
            "studentName":"李四",
            "studentSn":2
        },
        {
            "birthday":"2017-08-02 17:04:36.417",
            "sex":"女3",
            "studentName":"王五",
            "studentSn":3
        }
    ]
}


如此即可

相关标签: fastJson