Summary of JSON usage

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data exchange format. It uses a language-independent text format and can be used in
Data transmission between the user and the server! JSON objects can be used in both Ajax development and general J2EE development. They are used to submit more than one record at a time to the background! (For example, displayed on the page
Records in the table on)
Official JSON Website :(
JSON (JavaScript Object Notation) is a lightweight data
Interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of

JavaScript programming language, standard ECMA-262 3rd edition-December 1999. JSON is a text format that is completely language independent but uses conventions that are
Familiar to programmers of the C-family of languages, including C, C ++, C #, Java, JavaScript, Perl, Python, and other others. these properties make JSON an ideal data-interchange language.

JSON is built on two structures:
* A Collection of name/value pairs. In various ages, this is realized as an object, record, struct, dictionary,
Hash table, keyed list, or associative array.
* An ordered list of values. In most ages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or
Another. It makes sense that a data format that is interchangable with programming versions also be based
On these structures.

Simple Example:
1. VaR jsonobj = {person1: {name: "Jack", age: "12 "},
Person2: {name: "Kate", age: "23 "},
Person3: {name: "Jim", age: "14 "}
};
Call the attributes of a JSON object,
1) jsonobj. person1.name
2) jsonobj ["person1"]. Name
You can call every sub-object in a JSON object through a for loop.
For (var p in jsonstr ){
// Alert (typeof P); alert (p); // return string
STR + = jsonobj [p]. Name + "," + jsonobj [p]. Age + "<br> ";
}

2. VaR jsonobj2 = {persons: [{name: "Jordan", sex: "M", age: "40 "},
{Name: "Bryant", sex: "M", age: "28 "},
{Name: "McGrady", sex: "M", age: "27 "}
]};

Call the attributes of a JSON object,
1) jsonobj2.persons [0]. Name;
2) For Loop call method,
VaR persons = jsonobj2.persons; // The returned value is an array object.
For (VAR I = 0; I <persons. length; I ++ ){
Cur_person = persons [I];
STR + = cur_person.name + "'sex is" + cur_person.sex + "and age is" + cur_person.age + "<br> ";
}
Insert, delete, and update data to the persons array of jsonobj2,
Here, you can use the array object method of JavaScript to perform operations, as shown in figure
VaR person = {name: "Yaoming", sex: "M", age: "26 "};
Jsonobj2.persons. Push (person); // Add a record to the array
Jsonobj2.persons. Pop (); // Delete the last entry
Jsonobj2.persons. Shift (); // Delete the first item
Jsonobj2.persons. unshift (person); // Add a record at the beginning of the array
Any method suitable for javascript can be used in the array of JSON objects! So there is another method splice () for crud operations!
// Delete
Jsonobj2.persons. splice (0, 1); // start position, delete count
// Do not delete the replacement
VaR self = {name: "Tom", sex: "M", age: "24 "};
VaR brother = {name: "Mike", sex: "M", age: "29 "};
Jsonobj2.persons. splice (, self, Brother); // start position, delete count, insert object
// Replace and delete
VaR self = {name: "Tom", sex: "M", age: "24 "};
VaR brother = {name: "Mike", sex: "M", age: "29 "};
Jsonobj2.persons. splice (0, 1, self, Brother); // start position, delete count, insert object

2. I understand the basic JSON operations in Javascript, but it cannot interact with the background. Before that, I need to convert it to a string!

There are two methods,

1) introduce a method:

Function obj2str (o ){
VaR r = [];
If (typeof o = "string") Return "\" "+ O. replace (/([\ '\ "\])/g," \ $1 "). replace (/(\ n)/g, "\ n "). replace (/(\ r)/g, "\ r "). replace (/(\ t)/g, "\ t") + "\"";

If (typeof o = "object "){
If (! O. Sort ){
R [0] = "{"
For (var I in O ){
R [R. Length] = I;
R [R. Length] = ":";
R [R. Length] = obj2str (O [I]);
R [R. Length] = ",";
}
If (!! Document. All &&! /^ \ N? Function \ s * tostring \ (\) \ s * \ {\ n? \ S * \ [native code \] \ n? \ S * \} \ n? \ S * $/. Test (O. tostring )){
R [R. Length] = "tostring:" + O. tostring. tostring ();
R [R. Length] = ",";
}
R [R. Length-1] = "}"
} Else {
R [0] = "["
For (VAR I = 0; I <O. length; I ++ ){
R [R. Length] = obj2str (O [I]);
R [R. Length] = ",";
}
R [R. Length-1] = "]"
}
Return R. Join ("");
}
Return O. tostring ();
}

2) an open-source JSON parser and string converter provided by the official JSON website, JSON. js

So far, we can smoothly upload the records on the client side to the background !!

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.