Big talk front-end parsing JSON objects

Source: Internet
Author: User

One, for the standard JSON objects such as:

var result=[{"Flag": 1, "MacId": "2", "Mbid": 0, "userName": "XXX"},{"flag": 1, "MacId": "1", "Mbid": 1, "UserName" : "YYY"}];

When traversing, you can iterate through the array directly with a for loop, in one of two ways:

1. for(vari = 0; i < result.length; i++) {        //Result[i] means that the first JSON object is obtained as Jsonobject        //Result[i] The value of the specified field can be obtained by using the. Field nameResult[i].username; }2. for(varIinchresult) {        //represents the traversal of an array, and I represents the subscript value of an array ,        //Result[i] means that the first JSON object is obtained as Jsonobject        //Result[i] The value of the specified field can be obtained by using the. Field nameResult[i].username; }

Second, for non-standard JSON objects such as:

var result={"Datas": [{"Flag": 1, "MacId": "2", "Mbid": 0, "userName": "XXX"},{"flag": 1, "MacId": "1", "Mbid": 1, " UserName ":" YYY "}]};

The standard JSON array format ({},{}) must be parsed before traversing.

var data= Result.datas;
1. for(vari = 0; i < data.length; i++) {        //Data[i] means that the first JSON object is obtained as Jsonobject        //Data[i] The value of the specified field can be obtained by using the. Field nameData[i].username; }2. for(varIinchdata) {        //represents the traversal of an array, and I represents the subscript value of an array ,        //Data[i] means that the first JSON object is obtained as Jsonobject        //Data[i] The value of the specified field can be obtained by using the. Field nameData[i].username; }

Note: If you do not print it, first convert the JSON data to the object first, using the function json.parse (JSON data)

Big talk front-end parsing JSON objects

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.