JSON [JavaScript Object Notation]:javascript objects notation.
It is a lightweight data interchange format.
JSON is more convenient as a data format than XML in many contexts.
The data for JSON consists of the format of objects, arrays, and elements. Each of these formats can contain legitimate JavaScript data types.
In JavaScript, you can convert a string directly into JSON format by using the eval () method.
The JSON data source format is as follows:
Example one:
Copy Code code as follows:
{
"TableName": "Table name",
' Rows ': [{column 1: ' Value 1 '},{' column 2 ': ' Value 2 '} ... {"Column n": "Value N"}]
}
Example two:
Copy Code code as follows:
/* Code equivalent to JSON
var obj = new Object ();
Obj.createperson = function (_name,_age) {
THIS.name = _name;
This.age = _age;
}
Obj.getage = function () {
return this.age;
}
*/
var person = {
' Createperson ': function (_name,_age) {
THIS.name = _name;
This.age = _age;
},
' Getage ': function () {
return this.age;
}
};
Person.createperson ("Xugang", 20);
var p = person.getage ();
Alert (p);
Example three: the eval () method converts a string directly into JSON and gets the value of the element.
Copy Code code as follows:
<script type= "Text/javascript" >
<!--
Window.onload = function () {
var json_text = "{' book ': {' name ': ' Java programming ', ' Author ': [' Liu ', ' Xu ']}, ' num ': 222}";
Convert a string to an object using eval ()
var json_obj = eval ("+ Json_text +"));
Visit book-name
document.write (Json_obj.book.name);
Visit Book-author-xu
document.write (json_obj.book.author[1]);
}
-->
</script>
attachment: The $.getjson () method is provided in jquery to quickly access JSON data returned to the server side.