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.