Deep understanding of JSON data source formatting _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.