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. Previously I used the structure of JSON: var json1={"abc": [{"Name": "Txt1"},{"name", "Txt2"}]}; The method of traversal is: 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: for (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 into a method that will be called 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!
Http://caibaojian.com/json-length.html
JSON object length and traversal methods