【问题标题】:parseJSON will throw error on valid JSONparseJSON 将在有效 JSON 上抛出错误
【发布时间】:2013-06-10 08:23:13
【问题描述】:

我在尝试让 JQuery.parseJSON 函数解析以下 JSON 时遇到问题。 ("Uncaught SyntaxError: Unexpected token")

[{"ExtIdremoto":"8","ExtNombre":"Silla bebe","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"13.5","ExtCuantificable":"true"},{"ExtIdremoto":"9","ExtNombre":"Alzador","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"13.5","ExtCuantificable":"false"},{"ExtIdremoto":"10","ExtNombre":"Maxicosi","ExtDescripcion":"Lorem ipsum lorem pete can\r\n•\tBlue\r\n•\tBlue\r\n•\t“blue”","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"},{"ExtIdremoto":"12","ExtNombre":"GPS","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"}]

尽管 JSON 通过了http://jsonlint.com/ 验证,但看起来是“•”导致了问题。

我没有使用任何解析器库,我一直在尝试使用此处指定的函数来解决它: https://stackoverflow.com/a/16652683/1161355

我怎样才能让角色变身?

PD:我试图避免包含任何项目需求的外部库

更新

JSON 存储为字符串,并通过 session var 传递给 jsp。

StringBuilder sbJson = new StringBuilder();
sbJson.append("[");
Iterator<DatosExtra> it = datosDisponibilidad.getExtrasOpcionales().iterator();
while(it.hasNext()){
    DatosExtra datos = (DatosExtra) it.next();
    sbJson.append("{\"ExtIdremoto\":").append("\"").append(datos.getExtIdremoto()).append("\"").append(",\"ExtNombre\":").append(escaparCaracteresJSON(datos.getExtNombre(locale)))
                    .append(",\"ExtDescripcion\":").append(escaparCaracteresJSON(datos.getExtDescripcion(locale)))
                    .append(",\"ExtPrecioDia\":").append("\"").append(datos.getExtPrecioDia()).append("\"")
                    .append(",\"ExtPrecio\":").append("\"").append(datos.getExtPrecio()).append("\"")
                    .append(",\"ExtCuantificable\":").append("\"").append("S".equals(datos.getExtCuantificable())).append("\"")
                    .append("}");
    if(it.hasNext()) sbJson.append(",");
}
sbJson.append("]");
hpRequest.getRequest().getSession().setAttribute("jsonExtras", sbJson.toString());

其中 escaparCaracteresJSON 是前面注释的函数

在 JSP 中我恢复了值:

var jsonExtras = jQuery.parseJSON('<%=session.getAttribute("jsonExtras")%>');

输出就是这个:

var jsonExtras = jQuery.parseJSON('[{"ExtIdremoto":"8","ExtNombre":"Silla bebe","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"9","ExtCuantificable":"true"},{"ExtIdremoto":"9","ExtNombre":"Alzador","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"9","ExtCuantificable":"false"},{"ExtIdremoto":"10","ExtNombre":"Maxicosi","ExtDescripcion":"Lorem ipsum lorem pete can\r\n•\tBlue\r\n•\tBlue\r\n•\t“blue”","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"},{"ExtIdremoto":"12","ExtNombre":"GPS","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"}]');

【问题讨论】:

  • 不,这对我来说很好。你确定你没有做一些可能会去除反斜杠的事情吗?比如,你把那个 JSON 作为字符串写到你的 JS 中了吗?
  • 是的,我忘了说。 JSON 在 stringbuilder 中构建并存储在会话属性中,该属性在 JS var 中打印出来: var jsonExtras = jQuery.parseJSON('');

标签: java jquery json jakarta-ee encoding


【解决方案1】:

我相信您在 \r\n\t 中缺少一个反斜杠 - 它应该是 \\r\\n\\t。添加这些使 jQuery.parseJSON() 工作得很好 - http://jsfiddle.net/Nv282/。您能否说明如何在 escaparCaracteresJSON() 函数中转义字符?

【讨论】:

  • @GLlompart 此函数转义控制字符并使其成为 Java 中的有效字符串 - '\r' 字符变为 "\r" 字符串。由于您尝试在 Javascript 中使用此字符串,因此您需要一个额外的反斜杠来正确转义控制字符。更新您的 escaparCaracteresJSON() 函数以附加 "\\\\n" 而不是 "\\n"(对于其他控制字符也是如此)。这个想法是为输出字符串中的每个控制字符设置双反斜杠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多