eval

jquery 解析json与json 例子

风流意气都作罢 提交于 2019-11-29 17:01:17
jquery处理 网页特效 on的方法 jquery处理json的方法是直接eval,给一个变量,但是,今天看到其源代码,证明我的想法彻底错了。   它先判定有没有window.json.parse这个方法,如果有,直接用这个方法解析,如果没有,会new一个匿名函数,里面的内容,就是返回json的执行结果。   我试了一下,window.json这个对象,在firefox/webkit的浏览器中存在,但ie6-ie8都没有。   另外,如果大家需要解析json字符串,可以直接使用jquery的parsejson方法,虽然这个方法在手册中没有出现,例: var json_str = '{"a":1, "b":2}'; var data = $.parsejson(json_str); alert(data.a) 更详细的json处理方法与原理 jquery异步获取的数据类型—— 网页特效 on对象和字符串为依据,分别介绍两种方式获取到的结果处理方式。 1.对于服务器返回的网页特效on字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次。这种方式也适合以普通javascipt方式获取json对象,以下举例说明: var dataobj=eval("("+data+")");/

Ajax中解析Json的两种方法详解

≯℡__Kan透↙ 提交于 2019-11-29 17:00:36
eval(); //此方法不推荐 JSON.parse(); //推荐方法 一、两种方法的区别 我们先初始化一个json格式的对象: var jsonDate = '{ "name":"周星驰","age":23 }' var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法 var jsonObj = JSON.parse( jsonDate ); // JSON.parse(); 方法 然后在控制台调用: console.log( jsonObj.name ); // 两种方法都可以正确输入 周星驰 那么问题来了 两种方法有什么区别呢?(下面我们稍微把代码改动一下,蓝色字体为修改部分) var jsonDate = '{ "name":alert("hello"),"age":23 }' var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法 console.log( jsonObj.age ); //会先执行“alert”输出“hello” 然后才输出 23 换“JSON.parse();”方法: var jsonDate = '{ "name":alert("hello"),"age":23 }' var jsonObj = JSON.parse(

stringify、parse、param、eval、serialize、serializeArray的一些使用方法

拥有回忆 提交于 2019-11-29 17:00:18
测试代码: 1)JSON.parse() 将 JSON 字符串转换成对象: <html> <body> <script type="text/javascript"> var str = '{"name":"myName","id":"myId"}'; var obj=JSON.parse(str); console.info(obj); </script> </body> </html> 2)eval() 和 JSON.parse()一样都是json的的解析方法,把json字符串解析为一个object对象 var str = '{"name":"myName","id":"myId"}'; var obj= eval('(' + str + ')'); console.info(obj) 两者区别: eval()和JSON.parse() 的区别: eval()–解析字符串时会 执行 该字符串中的 代码 ,可以解析任何字符串(不安全) JSON.parse()–会对要解析的字符串进行格式检查,如果格式不正确则不进行解析(安全性高) 3)JSON.stringify()将对象转为JSON字符串 var obj={name:"myName",id:"myId"}; var str= JSON.stringify(obj); console.info(str); 4)jQuery

scope of eval function in python

纵然是瞬间 提交于 2019-11-29 16:35:23
问题 Consider the following example: i=7 j=8 k=10 def test(): i=1 j=2 k=3 return dict((name,eval(name)) for name in ['i','j','k']) It returns: >>> test() {'i': 7, 'k': 10, 'j': 8} Why eval does not take into consideration the variables defined inside the function? From the documentation, optionally you can pass a globals and a locals dictionary. What does it means?Finally, how can I modify this small case to make it work? 回答1: Generators are implemented as function scopes: The scope of names

vbscript Eval a string to a Variable in a loop?

倖福魔咒の 提交于 2019-11-29 16:33:10
I am trying to use vbscript's Eval (or maybe I need Execute) to create some variables from the key names from an ini file. The ini file can have unlimited unknown key=val pairs. I need to create a variable based on the key name no matter what. Ini File contents: myPath=c:\test myExe=myapp.exe .... xxx=123 yyy=abc My code that reads the ini and returns the key and values to an object The code I am trying to get working is here: For each pair in objINI Eval("pair.key=pair.val") Next msgbox myPath msgbox myExe But both msgbox's are showing empty And yes I am sure pair.key and pair.val have the

Python Class dynamic attribute access

喜你入骨 提交于 2019-11-29 16:30:14
I want to access a class attribute by a string with its name. Something like: class a: b=[] c='b' eval('a.'+c+'=1') But that doesn't work in Python. How can I do this? Use setattr : In [1]: class a: ...: b = [] ...: In [2]: setattr(a, 'b', 1) In [3]: a.b Out[3]: 1 Use getattr for the reverse: In [4]: getattr(a, 'b') Out[4]: 1 In [5]: getattr(a, 'x', 'default') Out[5]: 'default' I very much suspect, though, that there is a better way to achieve whatever your goal is. Have you tried using a dictionary instead of a class? BigOther eval is for evaluation, use exec to 'execute' statements: exec('a.

Dynamic variables matlab

試著忘記壹切 提交于 2019-11-29 16:18:17
How can I access to dynamic variables in Matlab? I search for similar question but I didn't find. Example (simplified): for i=1:1 aux3=(i-1)*50; delay_64_264(1,i) = mean(delay_64_264_', num2str(aux3), ' (:,3)*100; end What I want to do is mean of column 3 from variable delay_64_264_0 . Anyone can help me? Thank you very much You can use eval() . But I recommend not doing this at all. Use a multidimensional array, rather than lots of variables with slightly different names. To follow on from Oli's suggestions, see this piece of the MATLAB FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create

Question about eval in PHP 5

廉价感情. 提交于 2019-11-29 15:42:53
I have been doing PHP stuff for almost one year and I have never used the function eval() though I know the usage of it. But I found many questions about it in SO.So can someone show me a simple example in which it's necessary to use eval() ?And is it a good or bad practice? Using eval() is a bad practice, and if it turns out to be necessary to achieve something, that is usually the sign of a underlying design error. I can't think of any situation where it is necessary to use eval() . (i.e. something can't be achieved using other language constructs, or by fixing a broken design.) Interested

PHP eval issue with PHP + HTML code

天涯浪子 提交于 2019-11-29 15:37:45
I've got PHP and HTML code stored in a database table. When I get this data, I need to echo the HTML and process the PHP. I thought I could use eval() for this, which works, if I do this eval("echo 'dlsj'; ?> EVALED "); I get "dlsjEVALED" printed out. The problem is, I get a fatal error when I run longer scripts. Things like: Parse error: syntax error, unexpected '<' in /home/content.php(18) : eval()'d code on line 1 I would guess that you're trying to eval() something that contains an opening <?php tag. And that leads to the error at hand. Best advice - never store php and html code in your

Foreach loop unable to find object

我们两清 提交于 2019-11-29 15:29:50
I am trying to use foreach with the parallel backend to speed up computation (of cross validation of an {AUCRF} random forest for feature selection, if this does matter). In the process of doing so i need to get a subset of a vector. The name of the vector can change but is accessible as character vector. I used the eval(parse()) construct(good idea?) to get a subset of the vector. Example: library(parallel) library(foreach) library(stats) #create cluster clu <- makeCluster(detectCores() - 1) registerDoParallel(clu, cores = detectCores() - 1) bar<-c("a","b","c","d") rab<-c(2,3) bar.name<-"bar"