Gets the length or count of a JSON object

Source: Internet
Author: User

Recently began to write a blog. Because the recent work has begun to interface with the technical aspects. Now in the development of the web, often encounter JSON object delivery, JSON is a good thing, but it does not provide some simple and convenient processing method, which gets the length of the JSON object is one of the many problems encountered in the actual combat development.

Now, I'm giving out JavaScript code, why is it just a JavaScript code? Because other languages are more or less providing some work on JSON, but JavaScript does not provide much, and then in the Web development process, now the dependency on jquery more, so don't say more, see the code:

 1  function   Getjsonobjlength (jsonobj) { 2  var  Length = 0;  3   var  item in   Jsonobj) { 4  length++;  5   6
      return   Length;  7 } 

The use of the method is also very simple, we assume that there is a JSON object is "Jsontemp", the specific content is {["name": "Zhang San", "Age": 18],["name": "John Doe", "age": 19]}. We only need to do this when we are working with javascript:

1 var count = Getjsonobjlength (jsontemp);

To extend the application, such as the need to iterate through the JSON of each object read out, please do the following:

1  for (I=0;i<getjsonobjlength (jsontemp); i++) {2     name = jsontemp[i].name; 3     Age = Jsontemp[i].age; 4 }

Well, the above is the basic application, I hope to help you.

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.