Java 如何在json中转换日期

Java 如何在json中转换日期,java,json,Java,Json,我是json新手。 我使用的是一个mysql查询,其中有一个datetime字段。我想将查询结果转换为json。但是对于datetime字段,我将字段转换为仅正确执行的date.query并给出正确的输出。但是,当我尝试将此结果转换为json时,其给出的错误与日期时间字段相关。 要在json中转换的类是 package com.helical.efw.json; import java.util.List; import java.util.Map; import net.sf.json.JS

我是json新手。 我使用的是一个mysql查询,其中有一个datetime字段。我想将查询结果转换为json。但是对于datetime字段,我将字段转换为仅正确执行的date.query并给出正确的输出。但是,当我尝试将此结果转换为json时,其给出的错误与日期时间字段相关。 要在json中转换的类是

package com.helical.efw.json;

import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

public class ConvertJson {
    public String CommonJson(Map<String,? extends List<?>> commonList)
    {
        String jsonString;
        System.out.println("in json locationId List=======jjjj==========");
        JSONObject json = new JSONObject();
        json.accumulateAll(commonList);
        jsonString=json.toString();
        System.out.println(json.toString());
        return jsonString;
    }
}


请回复我

在JSON库中,有一个JSDateJSONBean处理器,但它会转换为

{
"minutes": 13,
"seconds": 14,
"hours": 12,
"month": 5,
"year": 2007,
"day": 17,
"milliseconds": 150
}
Jackson JSON处理器更灵活,默认情况下,它以毫秒为单位写入日期,但您也可以将其配置为使用ISO-8601格式,请参见

此答案适用于Google JSON java库

首先创建一个类型转换器类,如图所示

假设您正在使用,并且您的类型转换器类名为DateTimeTypeConverter,然后像这样创建Gson对象

 GsonBuilder builder = new GsonBuilder();
 builder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
 Gson gson = builder.create();                    

我就是这样做的。

您可以随时提供自己的
JsonValueProcessor
来实现所需的编码。我决定使用
getTime()
方法将日期编码为自1970年1月1日00:00:00GMT以来经过的毫秒

下面是一些示例代码:

JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {

    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        Date[] dates = (Date[])value;
        Long[] result = new Long[dates.length];
        for (int index = 0; index < dates.length; index++) {
            result[index] = dates[index].getTime();
        }
        return result;
    }

    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        Date date = (Date)value;
        return date.getTime();
    }

});

感谢greyfairer的回复,它给出了错误:“java.lang.reflect.InvocationTargetException类型异常报告”有关更多详细信息,我已在上面附上完整的错误日志,我不确定,但可能您必须单独格式化
DateTime
对象。检查相关链接谢谢alex,当我发送完整的时间戳时,会给出结果:{“儿童”:[{“时刻”:{“日期”:30,“天”:1,“小时”:14,“分钟”:2,“月”:11,“纳米”:0,“秒”:21,“时间”:1388392341000,“时区偏移”:-330,“年”:113},“t”:38}}}但我不需要这种类型的日期格式,我只需要YYYY-MM-DD格式的日期
 GsonBuilder builder = new GsonBuilder();
 builder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
 Gson gson = builder.create();                    
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {

    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        Date[] dates = (Date[])value;
        Long[] result = new Long[dates.length];
        for (int index = 0; index < dates.length; index++) {
            result[index] = dates[index].getTime();
        }
        return result;
    }

    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        Date date = (Date)value;
        return date.getTime();
    }

});
Map<String, Object> data = new HashMap<String, Object>();
data.put("myDate", new Date());
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( data, config );