eval

eval解析JSON中的注意点有哪些

我们两清 提交于 2019-11-27 11:39:23
 eval解析JSON中的注意点   在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:   1.一种为使用eval()函数。   2. 使用Function对象来进行返回解析。   使用eval函数来解析,并且使用jquery的each方法来遍历   用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject等插件封装的JSON对象, 管理软件开发平台 与此亦是大同小异,这里不再做说明。   这里首先给出JSON字符串集,字符串集如下:   代码如下:   var data="   {   root:   [   {name:'1',value:'0'},   {name:'6101',value:'北京市'},   {name:'6102',value:'天津市'},   {name:'6103',value:'上海市'},   {name:'6104',value:'重庆市'},   {name:'6105',value:'渭南市'},   {name:'6106',value:'延安市'},   {name:'6107',value:'汉中市'},   {name:'6108',value:'榆林市'},   {name

javascript eval和JSON之间的联系

ぐ巨炮叔叔 提交于 2019-11-27 11:39:11
如果您想详细了解eval和JSON请参考以下链接: eval : https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval JSON: http://www.json.org/ eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的字符串,并且试图去执行包含在字符串里的表达式或者一系列的合法的JavaScript语句。eval函数将把最后一个表达式或者语句所包含的值或引用作为返回值。 举例说明 eval评估JavaScript表达式 var bar = 'bar';var foobar = eval('"foo" + bar');alert(foobar); eval评估JavaScript语句 var bar = 'bar';// if variable bar equals 'bar', foobar is the result of // last executing statement: bar="foo-bar";var foobar = eval('if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}');alert(foobar);// change the

JSON 浅谈 — 3.0 解析

社会主义新天地 提交于 2019-11-27 11:38:59
解析,是指将符合 JSON 语法规则的字符串转换成对象的过程。 不同的编程语言都提供了解析 JSON 字符串的方法,在这里主要讲解 JavaScript 中的解析方法。主要有三种: 使用 eval() 使用 JSON.parse() 使用第三方库,例如 JQuery 等 1. eval() eval() 函数的参数是一个字符串,其作用是直接执行其中的 JavaScript 代码。 eval() 能够解析 JSON 字符串。从这里也可以看得出,JSON 和 JavaScript 是高度嵌合的。 但是,现在已经很少直接使用 eval() 来解析了,如果您的浏览器版本真的是很旧,可能才需要这个方法。此外,eval() 是一个相对危险的函数,因为字符串中可能含有未知因素。在这里,作为学习,还是要知道这也是一种方法。 JavaScript 请注意 eval() 的参数,在字符串两旁加了括号,这是必须的,否则会报错。 因为 JSON 字符串是被大括号(“{}”)包围的,直接放到 eval() 会被当成语句块来执行,因此要在两旁加上括号,使其变成表达式。 JavaScript 2. JSON.parse() 现在绝大多数浏览器都以支持 JSON.parse(),是推荐使用的方式。 JavaScript 如果输入了不符合规范的字符串,会报错。 JavaScript JSON.parse()

[JavaScript]高效 JavaScript

耗尽温柔 提交于 2019-11-27 10:54:43
作者 Mark 'Tarquin' Wilton-Jones · 2006年11月2日 本文翻译自 Efficient JavaScript 原译文地址 http://kb.operachina.com/node/207 传统上,网页中不会有大量的脚本,至少脚本很少会影响网页的性能。但随着网页越来越像 Web 应用程序,脚本的效率对网页性能影响越来越大。而且使用 Web 技术开发的应用程序现在越来越多,因此提高脚本的性能变得很重要。 对于桌面应用程序,通常使用编译器将源代码转换为二进制程序。编译器可以花费大量时间优化最终二进制程序的效率。Web 应用程序则不同。因为Web应用程序需要运行在不同的浏览器、平台和架构中,不可能事先完全编译。浏览器在获得脚本后要执行解释和编译工作。用户要求不仅要求网页能快速的载入,而且要求最终 Web 应用程序执行的效果要和桌面应用程序的一样流畅。Web 应用程序应能运行在多种设备上,从普通的桌面电脑到手机。 浏览器并不很擅长此项工作。虽然 Opera 有着当前最快的脚本引擎,但浏览器有不可避免的局限性,这时就需要 Web 开发者的帮助。Web开发者提高 Web 应用程序的性能的方法很多而且也很简单,如只需要将一种循环变成另一种、将组合样式分解成三个或者只添加实际需要的脚本。 本文从 ECMAScript/JavaScript, DOM,

what does eval do and why its evil? [duplicate]

前提是你 提交于 2019-11-27 09:21:27
This question already has an answer here: When is JavaScript's eval() not evil? 25 answers Why is using the JavaScript eval function a bad idea? 26 answers var myString = "x", myObject = { x: 10 }, value = eval("myObject." + myString); alert(value) alert(myObject[myString]); http://jslinterrors.com/eval-is-evil/ I have been reading about eval() function over the internet, but could not really grasp on what it actually does apart from " It Evaluates an expression ". Should we use eval() function only for numeric values?. Spudley eval() takes the string it is given, and runs it as if it were

JQuery getJSON - ajax parseerror

三世轮回 提交于 2019-11-27 08:23:46
I've tried to parse the following json response with both the JQuery getJSON and ajax: [{"iId":"1","heading":"Management Services","body":"<h1>Program Overview</h1><h1>January 29, 2009</h1>"}] I've also tried it escaping the "/" characters like this: [{"iId":"1","heading":"Management Services","body":"<h1>Program Overview <\/h1><h1>January 29, 2009<\/h1>"}] When I use the getJSON it dose not execute the callback. So, I tried it with JQuery ajax as follows: $.ajax({ url: jURL, contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function(x) { if(x && x.overrideMimeType)

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

橙三吉。 提交于 2019-11-27 08:13:35
I am getting the following error Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. but all I am trying to do is inside a ASP.NET REPEATER Control <% if ( Eval("Message").ToString() == HttpContext.Current.Profile.UserName) %> <% { %> <asp:ImageButton runat="server" etc.... /> <% } %> Steve The syntax is <%# Eval("...") %> You could do something like <asp:ImageButton Visible='<%# ShowImg(Eval(Container.DataItem,"Message")) %>' /> and in your codebehind: boolean ShowImg(string msg) { return (msg == HttpContext.Current.Profile.UserName)

Avoiding the infamous “eval(parse())” construct

我只是一个虾纸丫 提交于 2019-11-27 08:03:47
Ok, so I'm running some loops to process data stored in list objects. Ever mindful of the infamous fortune admonishment not to use eval(parse(mystring)) , I came up with this: Rgames> bar $foo $foo$fast [1] 1 2 3 4 5 $foo$slow [1] 6 7 8 9 10 $oof $oof[[1]] [1] 6 7 8 9 10 $oof[[2]] [1] 1 2 3 4 5 Rgames> rab<-'bar' Rgames> do.call('$',list(as.name(rab),'oof')) [[1]] [1] 6 7 8 9 10 [[2]] [1] 1 2 3 4 5 Typically I'd be selecting a list (of which bar is one such) and then one element of the list (e.g. oof ) which contains my data. The code above does the same thing as eval(parse(text=paste(rab,'$',

How to use Single Quotes in Eval Format String

£可爱£侵袭症+ 提交于 2019-11-27 07:46:34
问题 I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField. The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview. The SelectCommand has a WHERE clause which is to compare a string. Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause. How do I add single quotes inside an Eval FormatString? I have tried using 'Replace'. I have tried using 'Special

Is there a performance gain in including <script> tags as opposed to using eval?

和自甴很熟 提交于 2019-11-27 07:45:38
问题 I have seen a lot of suggestions about how one should add code dynamically like so (source): var myScript = document.createElement("script"); myScript.setAttribute("type","text/javascript"); myScript.innerHTML += 'alert("Hello");'; document.body.appendChild(myScript); As opposed to eval like so eval('alert("Hello");'); People complain about performance drops and security issues with eval , but I can't imagine how adding <script> tags would be any faster or any more secure. EDIT people would