JSON (JavaScript Object Notation) is a lightweight data exchange format, which uses a completely language-independent text format, it can be used to transmit data on the client and 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, records are displayed in the table on the page.)
introduction to the official JSON Website: ( www.json.org)
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 the 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 versions, 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 versions support them in one form or another. It makes sense that a data format that is interchangable with programming versions ages 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] = ",";
}< br> 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] = ",";
}< br> r [R. length-1] = "}"
}else {
r [0] = "["
for (VAR I = 0; I r [R. length] = obj2str (O [I]);
r [R. length] = ",";
}< br> r [R. length-1] = "]"
}< br> return r. join ("");
}< br> return O. tostring ();
}
I found it online, but it is quite helpful !! Thank you to the original creators !!