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

时间:2021-08-15 11:57:23

(如有错敬请指点,以下是我工作中遇到并且解决的问题)

= = = = = = = = = = = = = = = =  获取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对象的长度的方法:

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

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

alert(getJsonObjLength(str)) //2

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

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