如何在json中转换日期

时间:2014-01-20 11:21:37

标签: 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.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;
    }
}

触发查询的方法:

public List<Map<String,Object>> getLastSevenDaysStageExecutionDetail()
{
    System.out.println("I am in getLastStageExecutionDetail=========");
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Query query;
    List<Map<String,Object>> aliasToValueMapList=null;

    try{
        String d1="2013-12-30";
        String d2="2013-12-31";
        query =session.createSQLQuery("SELECT cast(moment as date) as dated, timestampdiff(second,moment, end_time) as t FROM efw_db.efw_job_time where moment between '"+d1+"' and '"+d2+"' group by cast(moment as date);");
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        aliasToValueMapList=query.list();
        System.out.println(aliasToValueMapList.toString());
    }catch(Exception e) {
        e.printStackTrace();
    }
    return aliasToValueMapList;
} 

     Error is:

HTTP Status 500 - java.lang.reflect.InvocationTargetException

type Exception report

message java.lang.reflect.InvocationTargetException

description The server encountered an internal error that prevented it from  fulfilling   this request.

例外

    net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
    net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
    net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    net.sf.json.JSONObject._fromMap(JSONObject.java:1168)
    net.sf.json.JSONObject.fromObject(JSONObject.java:163)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject._accumulate(JSONObject.java:2635)
    net.sf.json.JSONObject.accumulate(JSONObject.java:1560)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1582)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1564)
    com.helical.efw.view.StageExecution.LastSevenDaysStageWiseExecution(StageExecution.java:70)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

root cause

java.lang.reflect.InvocationTargetException
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
    org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
    org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
    org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
    org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
    net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:749)
    net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    net.sf.json.JSONObject._fromMap(JSONObject.java:1168)
    net.sf.json.JSONObject.fromObject(JSONObject.java:163)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject._accumulate(JSONObject.java:2635)
    net.sf.json.JSONObject.accumulate(JSONObject.java:1560)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1582)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1564)
    com.helical.efw.view.StageExecution.LastSevenDaysStageWiseExecution(StageExecution.java:70)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

root cause

java.lang.IllegalArgumentException
    java.sql.Date.getHours(Date.java:177)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
    org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
    org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
    org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
    org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
    net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:749)
    net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    net.sf.json.JSONObject._fromMap(JSONObject.java:1168)
    net.sf.json.JSONObject.fromObject(JSONObject.java:163)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    net.sf.json.JSONObject._accumulate(JSONObject.java:2635)
    net.sf.json.JSONObject.accumulate(JSONObject.java:1560)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1582)
    net.sf.json.JSONObject.accumulateAll(JSONObject.java:1564)
    com.helical.efw.view.StageExecution.LastSevenDaysStageWiseExecution(StageExecution.java:70)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 lo

请回复我

3 个答案:

答案 0 :(得分:6)

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

这是一些示例代码:

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();
    }

});

然后,您可以按如下方式创建JSONObject

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

答案 1 :(得分:2)

在JSON-Lib中,有一个JsDateJsonBeanProcessor,但那个转换为

{
"minutes": 13,
"seconds": 14,
"hours": 12,
"month": 5,
"year": 2007,
"day": 17,
"milliseconds": 150
}

Jackson JSON处理器更灵活,默认情况下将日期写为毫秒,但您也可以将其配置为使用ISO-8601格式,请参阅http://wiki.fasterxml.com/JacksonFAQDateHandling

答案 2 :(得分:1)

这个答案适用于Gson适用于java的Google Json库。

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

https://sites.google.com/site/gson/gson-type-adapters-for-common-classes-1

假设您正在使用joda time并且您的类型转换器类名为DateTimeTypeConverter,那么就像这样创建Gson对象

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

我这样工作。