从 String 解析到 Map 时出现 JsonParseException
我已经完成了这样的Map对象到字符串对象的转换
public String getJsonString(Map<String, Map<String,List<CalculateContentCount>>> countMap) {
Gson gson = new Gson();
String jsonString = gson.toJson(countMap);
return jsonString;
}
在ftl中我已将返回的String对象设置为请求并将其传递到JSP文件中
<#assign countMap = json>
<form action="/alfresco/jsp/kpub/reports/exportContentCountList.jsp" method="get">
<input type="hidden" name="countMap" id="countMap" value="${countMap}">
<input type="submit" value="ExportFiletoCSV"/>
</form>
在exportContentCountList中,我尝试将字符串对象解析回Map对象,
String jsonString = request.getParameter("countMap");
System.out.println("jsonString : "+jsonString);
Gson gson = new Gson();
Map<String,Map<String,List<CalculateContentCount>>> countMap = null;
Type type = null;
if(jsonString != null && !"".equals(jsonString)) {
type = new TypeToken<Map<String,Map<String,List<CalculateKpubContentCount>>>>(){}.getType();
countMap = gson.fromJson(jsonString,type);
}
在执行时,发生以下异常。这是例外情况
com.google.gson.JsonParseException: Failed parsing JSON source:
caused by:
com.google.gson.ParseException: Encountered "<EOF>" at line 1, column 1.
Was expecting one of:
<IDENTIFIER_SANS_EXPONENT> ...
<IDENTIFIER_STARTS_WITH_EXPONENT> ...
<SINGLE_QUOTE_LITERAL> ...
<DOUBLE_QUOTE_LITERAL> ...
"}" ...
可能是什么问题?
I have done Map object to string object conversion like this
public String getJsonString(Map<String, Map<String,List<CalculateContentCount>>> countMap) {
Gson gson = new Gson();
String jsonString = gson.toJson(countMap);
return jsonString;
}
In ftl I have set the returned String object into request and pased it into the JSP file
<#assign countMap = json>
<form action="/alfresco/jsp/kpub/reports/exportContentCountList.jsp" method="get">
<input type="hidden" name="countMap" id="countMap" value="${countMap}">
<input type="submit" value="ExportFiletoCSV"/>
</form>
In exportContentCountList, I tried to parse the string object back to Map object,
String jsonString = request.getParameter("countMap");
System.out.println("jsonString : "+jsonString);
Gson gson = new Gson();
Map<String,Map<String,List<CalculateContentCount>>> countMap = null;
Type type = null;
if(jsonString != null && !"".equals(jsonString)) {
type = new TypeToken<Map<String,Map<String,List<CalculateKpubContentCount>>>>(){}.getType();
countMap = gson.fromJson(jsonString,type);
}
While executing, following exception occurs. Here's the exceptiom
com.google.gson.JsonParseException: Failed parsing JSON source:
caused by:
com.google.gson.ParseException: Encountered "<EOF>" at line 1, column 1.
Was expecting one of:
<IDENTIFIER_SANS_EXPONENT> ...
<IDENTIFIER_STARTS_WITH_EXPONENT> ...
<SINGLE_QUOTE_LITERAL> ...
<DOUBLE_QUOTE_LITERAL> ...
"}" ...
What may be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查生成的 HTML 输出。右键单击网络浏览器中的页面并选择查看源代码。看起来合适吗?还有双引号? Gson 输出带有双引号的 JSON 字符串。换句话说,生成的 HTML 在语法上是无效的。
使用 JSTL
fn:escapeXml()
转义 HTML 特殊字符,例如<
、>
、"
、< code>' 这样它们就不会破坏 HTML 语法,这样整个字符串将被传回,而不是仅传回一部分,直到第一个引号(这导致 Gson 检测到 EOF(文件结束符,或最好解释为字符串结尾)。
更新:
如果您不能使用 JSTL,则必须使用
String#replace()
手动转义 HTML 实体。Check the generated HTML output. Rightclick page in webbrowser and choose View Source. Does it look right? Also the double quotes? Gson output JSON strings with double quotes. In other words, the generated HTML is syntactically invalid.
Use JSTL
fn:escapeXml()
to escape HTML special characters like<
,>
,"
,'
so that they won't malform the HTML syntax.This way the entire string will be passed back instead of only a part until the first quote (which caused Gson to detect an EOF (End Of File, or better to be interpreted as End Of String).
Update:
If you can't use JSTL, you have to use
String#replace()
to escape HTML entities manually. E.g.