首頁  >  文章  >  web前端  >  取得Json物件的長度以及判斷json物件是否為空

取得Json物件的長度以及判斷json物件是否為空

巴扎黑
巴扎黑原創
2016-12-06 13:24:09966瀏覽

假設json為: 

Json代碼  

[{"name":'a',"age":1,"sex":"0","title":"hello","state":"0","id":"1"},{"name":'b',"age":2,"sex":"1","title":"bye","state":"0","id":"2"}]


以下是獲取json物件的長度的方法: 

Js代碼  


Js代碼  

function getJsonObjLength(jsonObj) {  
    var Length = 0;  
    for (var item in jsonObj) {  
        Length++;  
    }  
    return Length;  
}

= = = = = = = = = = = = = = = = 判斷json物件是否為空= = = = = = = = = = = = = = = = = c



Js代碼  

alert(getJsonObjLength(str)) //2

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn