1

在这里,我在向 java 提交网格时遇到问题,我收到日期解析错误。如何解决此错误。请帮我。以下是我的回应。

这是回应

{"gridData":[{"id":170,"afs":5,"date":"04-22-2011","qty":12,"quantity":11,"shift":"A","sourceLocation":"Office","time":"12:00:00 AM"},{"id":171,"afs":6,"date":"04-22-2011","qty":12,"quantity":11,"shift":"B","sourceLocation":"Office","time":"12:00:00 AM"},{"id":172,"afs":7,"date":"04-22-2011","qty":12,"quantity":11,"shift":"C","sourceLocation":"Office","time":"12:00:00 AM"},{"id":173,"afs":8,"date":"04-22-2011","qty":12,"quantity":11,"shift":"A","sourceLocation":"Office","time":"12:00:00 AM"},{"id":174,"afs":9,"date":"04-22-2011","qty":12,"quantity":11,"shift":"B","sourceLocation":"Office","time":"12:00:00 AM"},{"id":175,"afs":10,"date":"04-22-2011","qty":12,"quantity":11,"shift":"C","sourceLocation":"Office","time":"12:00:00 AM"},{"id":176,"afs":11,"date":"04-22-2011","qty":12,"quantity":11,"shift":"A","sourceLocation":"Office","time":"12:00:00 AM"},{"id":177,"afs":12,"date":"04-22-2011","qty":12,"quantity":11,"shift":"B","sourceLocation":"Office","time":"12:00:00 AM"},{"id":178,"afs":13,"date":"04-22-2011","qty":12,"quantity":11,"shift":"C","sourceLocation":"Office","time":"12:00:00 AM"},{"id":179,"afs":14,"date":"04-22-2011","qty":12,"quantity":11,"shift":"A","sourceLocation":"Office","time":"12:00:00 AM"},{"id":180,"afs":15,"date":"04-22-2011","qty":12,"quantity":11,"shift":"B","sourceLocation":"Office","time":"12:00:00 AM"},{"id":181,"afs":16,"date":"04-22-2011","qty":12,"quantity":11,"shift":"C","sourceLocation":"Office","time":"12:00:00 AM"},{"id":182,"afs":17,"date":"04-22-2011","qty":12,"quantity":11,"shift":"A","sourceLocation":"Office","time":"12:00:00 AM"}],"success":true}

这是网格列配置

columns: [
            {
                xtype: 'gridcolumn',
                dataIndex: 'afs',
                text: 'AFS'
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'date',
                text: 'Date',
                flex :1
            }]

这是我的日期 POJO 类配置

@JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
    return this.date;
}

这是我的控制器类

 public@ResponseBody String saveGridData(HttpServletRequest request,@RequestParam("grid") String gridData){
    System.out.println("Group Name :"+request.getParameter("groupName"));
    JSONPObject json = new JSONPObject("encode", gridData);
    System.out.println(gridData);
    NewSand[] newSands =    getJsonString(gridData, NewSand[].class);
    for(NewSand newSand: newSands){
        System.out.println("Shift --> " + newSand.getShift());
    }
    return "true";
}

private static <T> T getJsonString(String json,  Class<T> type) throws JsonParseException{

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.serializeNulls();
    JsonSerializer<Date> ser = new JsonSerializer<Date>() {
          public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext 
                     context) {
            return src == null ? null : new JsonPrimitive(src.getTime());
          }
        };

        JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
          public Date deserialize(JsonElement json, Type typeOfT,JsonDeserializationContext context) throws JsonParseException {
            return json == null ? null : new Date(json.getAsLong());
          }
        };
        Gson gson = gsonBuilder.registerTypeAdapter(Date.class, ser).registerTypeAdapter(Date.class, deser).create();
    //Gson gson = gsonBuilder.create();
    return (T) gson.fromJson(json, type);   
}

异常代码是:

    Jul 03, 2012 2:55:46 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/Login] threw  exception [Request processing failed; nested exception is java.lang.NumberFormatException: For  input string: "04-22-2011"] with root cause
 java.lang.NumberFormatException: For input string: "04-22-2011"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.parseLong(Long.java:483)
at com.google.gson.JsonPrimitive.getAsLong(JsonPrimitive.java:233)
at com.mpm.common.controller.DataUploadController$2.deserialize(DataUploadController.java:248)
at com.mpm.common.controller.DataUploadController$2.deserialize(DataUploadController.java:1)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
at com.google.gson.Gson.fromJson(Gson.java:791)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)
at com.mpm.common.controller.DataUploadController.getJsonString(DataUploadController.java:253)
at com.mpm.common.controller.DataUploadController.saveGridData(DataUploadController.java:228)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

我收到日期解析错误,请帮助我提前谢谢。

4

1 回答 1

2

错误很明显:NumberFormatException: For input string: "04-22-2011"]您传入的值应该是数字,但实际上是日期格式。

我认为这是你做的地方:json.getAsLong(),它希望有一个像 '12345654' 这样的字符串,但它得到的是日期字符串。

编辑:

而不是这样做:

new Date(json.getAsLong())

尝试:

new Date(json.getAsString())
于 2012-07-03T10:17:49.793 回答