1

我想将 json 值传递给 servlet 以处理请求。

在客户端,我可以制作带有特殊字符的对象,例如 "&" 。

我已经给出了输入

"<h3>Welcome to ERP your name is #NAME#</h3>\n\n<h3>&nbsp;</h3>\n" 

我通过使用从 json 对象中检索值

request.getParameter("jsonVal") 

通过这样调用," " 之后的 json 值,不被 servlet 获取。输出是

"\"<h3>Welcome to ERP your name is #NAME#</h3>\\n\\n<h3>

并将错误显示为:

org.json.JSONException: Unterminated string at character 206
at org.json.JSONTokener.syntaxError(Unknown Source)
at org.json.JSONTokener.nextString(Unknown Source)
at org.json.JSONTokener.nextValue(Unknown Source)
at org.json.JSONObject.<init>(Unknown Source)
at org.json.JSONTokener.nextValue(Unknown Source)
at org.json.JSONArray.<init>(Unknown Source)
at org.json.JSONTokener.nextValue(Unknown Source)
at org.json.JSONObject.<init>(Unknown Source)
at org.json.JSONTokener.nextValue(Unknown Source)
at org.json.JSONObject.<init>(Unknown Source)
at org.json.JSONObject.<init>(Unknown Source)
at erp.controller.Controller.doPost(Controller.java:49)
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.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:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
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:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

如何处理这个问题?

提前致谢..

4

1 回答 1

0

获取 request.getParameter("jsonVal") 后,尝试使用 JSON 查看器并确定特殊字符是什么。回车+新行与 JSONTokener 有问题,因此您可以尽可能修剪它并使用它。

于 2019-07-02T13:47:24.570 回答