ev
JSON:http://www.json.org/
ev
ev
舉例說明
var bar = 'bar';
var foobar = eval('"foo" + bar');
alert(foobar);
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 valuebar = 'foo';
// now our the last executed statement is: bar = "bar-foo";
// therefore the value of variable foobar has been changed
// into 'bar-foo'
foobar = eval('if(bar == "bar") {bar="foo-bar";} else {bar = "bar-foo";}');
alert(foobar);
JSON的格式
JSON的格式是由大括號和由冒號(:)構成的名值對所組成的。注意JSON格式與對象字面量 (object literals) 的區別:JSON的名字部分嚴格用引號+名字來表示。
舉例說明
var objectLiteral = {
name: "Objector.L",
age: "24",
special: "JavaScript",
sayName: function() {
return this.name;
}
};
var jsonFormat = {
"summary": "Blogs",
"blogrolls": [
{
"title": "Explore JavaScript",
"link": "http://example.com/"
},
{
"title": "Explore JavaScript",
"link": "http://example.com/"
}
]
};
ev
由于Ajax的興起,JSON這種輕量級的數據格式作為客戶端與服務器之間的傳輸格式逐漸地流行起來,進而出現的問題是如何將服務器端構建好的JSON數據轉化為可用的JavaS
var jsonObject = eval("(" + jsonFormat + ")");
為什么要加括號?
加上圓括號的目的是迫使ev
alert(eval("{}"); // return undefined
alert(eval("({})");// return object[Object]
JSON格式的名字部分為什么要加引號?
因為ev
舉例說明
alert(eval('{foo:"bar"}')); // return "bar", incorrect
alert(eval('({"foo": "bar"})')); // return JSON object, correct
結論
理解ev
following this format:
eval('{' + jsonString + ')');
新聞熱點
疑難解答