我在“file.text”中有一个包含以下信息的文件我将文件放在webcontent文件夹中。
var data = [
{
"id": "1",
"name": "Mike",
"Start_Date": "Sun 10/10/13",
"Finish_Date": "Sun 12/12/13",
"Position": "Trainee"
},
{
"id": "2",
"name": "Jhon",
"Start_Date": "Sun 10/10/13",
"Finish_Date": "Sun 12/12/13",
"Position": "Developer"
},
{
"id": "3",
"name": "Pedro",
"Start_Date": "Sun 10/10/13",
"Finish_Date": "Sun 12/12/13",
"Position": "Trainee"
}
]
我必须使用dopost方法读取和更新servlet类中的文件。但我得到例外。请支持!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Invoked");
Gson gson = new Gson();
ServletContext ctx = getServletContext();
JsonReader reader = new JsonReader(new FileReader(ctx.getRealPath("file.text")));
MyyData[] myyData = gson.fromJson(reader, MyData.class);
System.out.println(activityData);
response.getWriter().write("response");
}
异常消息:
Invoked
Jan 28, 2014 12:54:03 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [fi.aalto.dcse.visilean.devTestServlet] in context with path [/DevTestProject] threw exception
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at fi.aalto.dcse.visilean.devTestServlet.doPost(devTestServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
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)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
... 23 more
答案 0 :(得分:2)
您正在传递MyData数组,但尝试将它们转换为MyData。 以下将有效。
MyyData[] myyData = gson.fromJson(reader, MyData[].class);
GSON
建议的另一种方法是使用正确的TypeTokens。
Type myDataType = new TypeToken<Collection<MyData>>(){}.getType();
Collection<MyData> myData = gson.fromJson(json, myDataType);
参考: https://sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples