JSON object length and traversal methods

Source: Internet
Author: User
Tags chrome developer chrome developer tools

Excerpt from the blog (http://caibaojian.com/json-length.html)

Original link: http://caibaojian.com/json-length.html

The JSON array has a length of json.abc.length, and if it is purely JSON format, you cannot get the length directly using the Json.length method, but you should use other methods.

Recently in the modification of an HTML page js to traverse the JSON object, but how also debugging does not pass. Blame this HTML page do not know what method to prohibit the JS error hint, at the beginning of the time do not know that the problem, with chrome developer tools have not found the error, is to get Json.length is always undefined, so I think the JSON method you defined is wrong. Tossing a night one o'clock no progress, head all dizzy, too late, finally disappointed to wash and sleep.

The next night is not reconciled to this small problem can baffle me, so calm down to carefully check the reason. Create a new empty HTML file in which to test, found that the JSON object does not have the length property at all, in search engine lookup also no more authoritative reference document mentioned JSON has this attribute. Blogs also rarely mention json.length. But the strange thing is I remember the JSON has length, I also used, the program is correct, run correctly.

What the hell was that all about? Finally read a blog article when recalling the details of the previous programming, remember that the previous JSON sub-object is the number of groups and this time is not, this finally dawned.

I used to use this structure of JSON:

var json1={"abc": [{"Name": "Txt1"},{"name", "Txt2"}]};

The Via traversal method is: original from: http://caibaojian.com/json-length.html

for (Var i=0;i<json1.abc.length;i++) {alert (json1.abc[i].name);}

Here the JSON1.ABC is an array, the array is composed of 2 sub-JSON, the array is a length property, so say can work.

And my JSON this time is this:

var json2={"name": "Txt1", "name2": "Txt2"};

The JSON itself does not have the length property, so the length property naturally makes an error:

for (Var i=0;i<json2.length;i++) {alert (json2[i].text);}

What about traversing such json? Do this:

Code from Http://caibaojian.com/json-length.htmlfor (Var js2 in Json2) {alert (js2+ "=" +json2[js2]);}

Since JSON has no length property, what if you want to know what his length is? It is simple to change the above traversal:

var jslength=0;for (var js2 in json2) {jslength++;}

write this code in a way that you can call later:

function Getjsonlength (jsondata) {var jsonlength = 0;for (var item in jsondata) {jsonlength++;} return jsonlength;}

Learning computer programming technology must be rigorous and serious, can not tolerate the slightest careless, learning the most important is to lay the foundation, swallowed, superficial understanding will give work and learning to bring a lot of hidden trouble, efficiency must be discounted. Write it down to encourage each other!

JSON object length and traversal methods

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.