Basic operations examples of JSON objects in JavaScript _ basics

Source: Internet
Author: User

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:

var global = this;

In JavaScript, any independent function or variable belongs to the object's properties, namely:

function Test () {}

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);}
  }}

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.