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 に答える