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

JSONConfig处理日期

程序员文章站 2022-07-12 15:53:13
...

javaBean转JSON对象的时候,使用JSONObject.fromObject(object)的时候,针对日期转换显示的不是我们想要的效果。具体详情见如下测试代码:

javaBean对象

package com.zhdw.mgrclient.test;

import java.util.Date;

public class Person {
    private String name;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }



}

测试类:

package com.zhdw.mgrclient.test;

import java.util.Date;

import net.sf.json.JSONObject;

public class JSONTest {

    public static void main(String[] args) {
        Person hbk = new Person();
        hbk.setAge(30);
        hbk.setName("黄宝康");
        hbk.setBirthday(new Date());
        JSONObject object = JSONObject.fromObject(hbk);
        System.out.println(object);
    }
}

运行输出的结果:

{"birthday":{"time":1526873584591,"minutes":33,"seconds":4,"hours":11,"month":4,"year":118,"timezoneOffset":-480,"day":1,"date":21},"name":"黄宝康","age":30}

如果我们想输出yyyy-MM-dd格式,需要配合使用JsonConifg类

package com.zhdw.mgrclient.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonDateValueProcessor implements JsonValueProcessor{
    private String format ="yyyy-MM-dd";

    public JsonDateValueProcessor() {  
        super();  
    }  

    public JsonDateValueProcessor(String format) {  
        super();  
        this.format = format;  
    }  

    @Override
    public Object processArrayValue(Object object, JsonConfig arg1) {
        // TODO Auto-generated method stub
        return process(object);
    }

    @Override
    public Object processObjectValue(String arg0, Object object, JsonConfig arg2) {
        // TODO Auto-generated method stub
        return process(object);
    }

    private Object process(Object value){  
        if(value instanceof Date){    
            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);    
            return sdf.format(value);  
        }    
        return value == null ? "" : value.toString();    
    }  

}

新建一个JsonDateValueProcessor 类实现JsonValueProcessor接口,在使用JSONObject.fromObject(object)的时候,多传入一个JsonConfig配置对象。

代码如下:

package com.zhdw.mgrclient.test;

import java.util.Date;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JSONTest {

    public static void main(String[] args) {
        Person hbk = new Person();
        hbk.setAge(30);
        hbk.setName("黄宝康");
        hbk.setBirthday(new Date());
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
        JSONObject object = JSONObject.fromObject(hbk,config);
        System.out.println(object);
    }
}

运行后控制台输出:

{"birthday":"2018-05-21","name":"黄宝康","age":30}