The JSON format enables objects (object), arrays (array), values (value), strings (string), numeric values (number).
Copy Code code 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"}]//include array
}
Alert (user. Name);
Alert (user. Phone.mobilephone);
alert (USER.BABY[0].BIG);
User.baby[0].big = "--";//Modified JSON value
alert (USER.BABY[0].BIG);
</script>
Reference: http://www.json.org/json-zh.html
Here's a look at the JavaScript eval function.
The Eval function evaluates the string of a given JavaScript code and attempts to execute an expression contained in a string or a series of legitimate JavaScript statements. The Eval function returns the value or reference contained in the last expression or statement as the return value.
Code
Copy Code code as follows:
<script type= "Text/javascript" >
var bar = "Bar";
var foobar = eval (' foo ' +bar ');
Alert (bar + foobar);//EXECUTE statement
Foobar = eval (' If bar = ' bar ') {bar = ' foo-bar ';} Else{bar = "Bar-foo";} ');
Alert (foobar)//Returns the value contained in the statement
</script>
The difference between JSON and object literals (objects literals): The name part of JSON is strictly expressed in quotes + names.
Code
Copy Code code as follows:
<script type= "Text/javascript" >
Object literal Amount
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>
Thanks to the rise of Ajax, the lightweight data format of JSON became popular as a form of transmission between the client and server, and the problem was how to convert the server-side-built JSON data into usable JavaScript objects, using the eval A function is undoubtedly a simple and straightforward method. When converting, you need to wrap the outside of the JSON string with a bracket:
var jsonobject = eval ("(+ Jsonformat +)")
The purpose of the bracket is to force the Eval function to force an expression in parentheses (expression) into an object, rather than as a statement (statement), when evaluating JavaScript code. For example, an object literal {}, if the outer bracket is not well added, then Eval will mark the curly brace as the beginning of the JavaScript block, then {} will be considered to have executed an empty statement. So the following two execution results are different:
Copy Code code as follows:
Alert (eval ("{}")); return undefined
Alert (eval ("({})")); Return [Object Object]
What is the name part of the JSON format to quote? Because the Eval function interprets {foo: "Bar"} as a legitimate JavaScript statement, not an expression. But people tend to want eval to interpret this code as an object. So the JSON format will force you to enclose the outer side of the name with quotes and parentheses, and eval will not incorrectly interpret JSON as a block of code.
Copy Code code as follows:
Eval Error parsing semantics
Alert (eval (' {foo: ' Bar '} ')); Return "Bar", incorrect
Eval parses JSON correctly
Alert (eval ({"foo": "Bar"}));//return JSON Object,correct