Home  >  Article  >  Web Front-end  >  Get the length of the Json object and determine whether the json object is empty

Get the length of the Json object and determine whether the json object is empty

巴扎黑
巴扎黑Original
2016-12-06 13:24:09970browse

Assume that json is:

Json code

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


The following is the method to get the length of the json object:

Js code

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

You can get it through this method.

Js code

alert(getJsonObjLength(str)) //2


= = = = = = = = = = = = = = Determine whether the json object is empty = = = = = = = = = = = = = = = =

Js code

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn