0

Please take a look at this javascript code inside a GSP(similar to JSP):

var json =JSON.parse("${savedkpiz.get(0).kpi.replace("\"","\\\"")}")

savedkpiz(list object) object is sometimes have no element so access at 0 will throw NPE, how can i prevent this code from executing?? JavaScript if else seems not working

var json =JSON.parse("${if(savedkpiz.size()>0) ? savedkpiz.get(0).kpi.replace("\"","\\\""):""}")

above code too not working?? how can i put condition on this and at the same time if true then populate json variable.

Please help guyz, thanks in advance

4

1 回答 1

1

<c:if>您可以通过使用 JSTL 的标记并在 JavaScript 中进行字符串替换来拆分您的逻辑并实现您想要的。

var jsonStr = "";

<c:if test="${not empty savedkpiz}">
    jsonStr = "${savedkpiz[0]}".replace(/"/g, "\\\"");
</c:if>

var json = JSON.parse(jsonStr);
于 2013-09-16T13:25:44.273 回答