项目中用到JSON,以前也只是听说过,这是我第一次用,现在有一个问题,贴出来作为积累,也很是希望能有高手指点一下.
在JSP页面中,有这样的一段javascript代码:
<script Language="javascript">
var menulist = eval(<c:out value='${model.menuListString}' escapeXml='false' />);
</script>
这里的eval()方法也是第一次用,Google后找到了下面的说明(网址为http://today.java.net/pub/a/today/2006/04/27/building-ajax-with-dojo-and-json.html#what-is-json):
JSON is a Java library that helps convert Java objects into a string representation. This string, when eval()ed in JavaScript, produces an array that contains all of the information that the Java object contained. JSON's object notation grammar is suitable for encoding many nested object structures. Since this grammar is much smaller than its XML counterpart, and given the convenience of the eval() function, it is an ideal choice for fast and efficient data transport between browser and server.
可这个eval()是具体干什么的?在下面的这个
var menulist = eval(<c:out value='${model.menuListString}' escapeXml='false' />);
代码里把eval()删去后没发现什么影响.
在http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_eval页面中看到下面的演示代码:
<script type="text/javascript">
eval("x=10;y=20;document.write(x*y)");
document.write("<br />");
document.write(eval("2+2"));
document.write("<br />");
var x=10;
document.write(eval(x+17));
document.write("<br />");
eval("alert('Hello world')");
</script>
可这与JSON又有什么关系呢?