As a front-end page developer for the first time to process JSON data, encountered the ' JS gets the length of the array inside the JSON array '? I don't know if JSON has no. Length property (really want to taunt yourself), an idle youth needy age AH! I used to seek help from my boyfriend, but recently tried to solve the problem.
My problem is this: ***.jsp end of the page, the back end of the JSON data passed to me is an array, and then to get the second array contacts inside the array length, and then Baidu found that JSON does not. Length property. What do we do?
Since the JSON object has no length property, what if you want to know what his length is?
var jslength=0; for (var in json) { jslength+ +; }
Write this code in a way that you can call later:
function Getjsonlength (jsondata) { var0; for (var in jsondata) { jsonlength+ +; } return jsonlength;}
But the above method can only get to the length of the first level array??? Cannot get the array length inside the Subarray??? Then self-played, using the following method to obtain the length of the contacts array successfully.
var_data =${contactjson};function getjsonlength (jsondata) {varJsonlength =0; for(varIteminchjsondata) {if(Item = ='Contacts'){ for(varXinchJsondata[item]) {Jsonlength++; }}}returnjsonlength;}var_contact_num =getjsonlength (_data); $ ('#contactNum'). Text (_contact_num);
The result of the last call:
JS gets the length of the array inside the JSON array