首页  >  文章  >  web前端  >  获取Json对象的长度以及判断json对象是否为空

获取Json对象的长度以及判断json对象是否为空

巴扎黑
巴扎黑原创
2016-12-06 13:24:09967浏览

假设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代码  

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

通过该方法就可以获取到。 

Js代码  

alert(getJsonObjLength(str)) //2


= = = = = = = = = = = = = = = = 判断json对象是否为空 = = = = = = = = = = = = = = = = = = 

Js代码  

var json={a:12,b:45}  
for(var key in json){  
    alert(key);       //a, b  
    alert(json[key]); //12, 45  
}


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn