Home > Web Front-end > JS Tutorial > body text

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

巴扎黑
Release: 2016-12-06 13:24:09
Original
974 people have browsed it

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"}]
Copy after login


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;  
}
Copy after login

You can get it through this method.

Js code

alert(getJsonObjLength(str)) //2
Copy after login


= = = = = = = = = = = = = = 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  
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!