In JSON format, objects, arrays, values, strings, and numbers can be used ).
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var user = {
"Name": "Andy ",
"Sex": "man ",
"Age": "20 ",
"Phone": {"mobilephone": "123456789", "telephone": "1234-12345678 "},
"Baby": [{"big": "xiaohong" },{ "middle": "xiaoming" },{ "small": "xiaoli"}] // contains an array
}
Alert (user. Name );
Alert (user. Phone. mobilephone );
Alert (user. baby [0]. big );
User. baby [0]. big = "--"; // modified the JSON value.
Alert (user. baby [0]. big );
</Script>
Reference: http://www.json.org/json-zh.html
Let's take a look at the JavaScript eval function.
The eval function evaluates a given string of JavaScript code and tries to execute expressions contained in the string or a series of legal JavaScript statements. The eval function uses the value or reference contained in the last expression or statement as the return value.
Code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var bar = "bar ";
Var foobar = eval ('"foo" + bar ');
Alert (bar + foobar); // execute the statement
Foobar = eval ('If (bar = "bar") {bar = "foo-bar";} else {bar = "bar-foo ";}');
Alert (foobar); // return the value contained in the statement
</Script>
The difference between JSON and Object literal (Object Literals): The JSON name is strictly represented by quotation marks + names.
Code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Object literal
Var objectLiteral = {
Name: "Objector. L ",
Age: "24 ",
Special: "JavaScript ",
SayName: function (){
Return this. name;
}
};
// JSON object
Var jsonFormat = {
"Summary": "Blogs ",
"Blogrolls ":[
{
"Title": "Explore JavaScript ",
"Link": "http://example.com"
},
{
"Title": "Explore JavaScript ",
"Link": "http://example.com"
}
]
};
</Script>
Due to the rise of Ajax, the lightweight JSON data format is gradually becoming popular as the format for transmission between the client and the server, the problem is how to convert the JSON Data Built on the server to available JavaScript objects. Using the eval function is a simple and direct method. During conversion, enclose the JSON string with square brackets:
Var jsonObject = eval ("(" + jsonFormat + ")")
The purpose of parentheses is to force the eval function to forcibly convert the expressions in the brackets into objects when evaluating JavaScript code, rather than being executed as statements. For example, if the object literal quantity {} is not properly added with outer brackets, eval will mark the braces as the beginning and end of the JavaScript code block, then {} is considered to have executed an empty statement. The following two execution results are different:
Copy codeThe Code is as follows:
Alert (eval ("{}"); // return undefined
Alert (eval ("({})"); // return [object Object]
Why do I need to quote the JSON format name? Because the eval function interprets {foo: "bar"} as a legal JavaScript statement, not an expression. But people often want eval to interpret this code as an object. Therefore, the JSON format forces you to add quotation marks on the outer side of the name, and then combine the parentheses, eval will not incorrectly interpret the JSON as a code block.
Copy codeThe Code is as follows:
// Eval error parsing Semantics
Alert (eval ('{foo: "bar"}'); // return "bar", incorrect
// Parse JSON correctly using eval
Alert (eval ('({"foo": "bar"})'); // return JSON object, correct