JSON object
1, the properties of the object:
the properties of an object are composed of key-value pairs, where key is a string and value can be any JavaScript object.
Use [] to set and get properties of an object
var obj = new Object ();
obj["www.jb51.net"] = "http://www.jb51.net";
Alert (obj["www.jb51.net"]);
2. Variables are both attributes:
the JavaScript engine constructs a global object when initialized, and all variables are properties of this global object. To refer to this global object, you can then get this in the top-level scope:
In JavaScript, any independent function or variable belongs to the object's properties, namely:
Equivalent:
Window.test = function () {}
3, the use of the object:
three ways to declare an object:
① creates an object object by using the new operator, and then dynamically adds the property, creating a target from scratch
② defines the class circle of the object, and then uses the new operator to bulk construct the object
Create an object
function User (username, password) {
this.username = Username;
This.password = password;
This.getusername = function () {return
this.username;
}
This.getpassword = function () {return
this.password;
}
}
var arthinking = new User ("Jason", "123");
Alert (Arthinking.getusername ());
Alert (Arthinking.getpassword ());
③ using JSON to construct objects
JSON is the JavaScript object representation method (JavaScript Object notation), which means that an object is represented by a literal amount:
JSON form to create an object
var arthinking = {
username: "Jason",
Password: "123",
favorite: {
sports: "F Ootball ",
Music:" Guitar "
}
alert (arthinking.username);
alert (arthinking.favorite.sports);
Resolves JSON-formatted data returned by the server
a single JSON object:
[{A: ' 1 ', B ' 2 '},{a: ' 3 ', B ' 4 '}]
Multiple JSON objects:
{
"usergroups": [{a: ' 001 ', B: ' arthinking '},a: ' 002 ', B: ' Jason '}],
"groups": [{c: ' 001 ', D: ' It Curtilage '}]
}
Can be based on this format from the background to encapsulate the need to pass the data, the foreground can be obtained after the resolution to obtain data:
Suppose Response.responsetext
converts a JSON string to a JavaScript statement//by using the Eval () function for the returned JSON string//
and then through "." Navigate to get the specific properties, the length property is the lengths of the object
var obj = eval ("+ Response.responsetext +"));
for (var i = 0; i<obj.usergroups.length; i++) {
var groupid = obj.usergroups[i].a;
var usergroup=obj.groups;
for (var j=0; j<usergroup.length; J + +) {
if (usergroup[j].c = = GroupID) {
alert (groupid);}
}}