从 String 解析到 Map 时出现 JsonParseException

发布于 2024-10-07 04:26:57 字数 1572 浏览 3 评论 0原文

我已经完成了这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-10-14 04:26:57

检查生成的 HTML 输出。右键单击网络浏览器中的页面并选择查看源代码。看起来合适吗?还有双引号? Gson 输出带有双引号的 JSON 字符串。换句话说,生成的 HTML 在语法上是无效的。

使用 JSTL fn:escapeXml() 转义 HTML 特殊字符,例如 <>"、< code>' 这样它们就不会破坏 HTML 语法,

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input type="hidden" name="countMap" id="countMap" value="${fn:escapeXml(countMap)}">

这样整个字符串将被传回,而不是仅传回一部分,直到第一个引号(这导致 Gson 检测到 EOF(文件结束符,或最好解释为字符串结尾)。


更新

如果您不能使用 JSTL,则必须使用 String#replace() 手动转义 HTML 实体。

return jsonString.replace("\"", """);

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.

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input type="hidden" name="countMap" id="countMap" value="${fn:escapeXml(countMap)}">

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.

return jsonString.replace("\"", """);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文