Home > Web Front-end > JS Tutorial > 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

巴扎黑
Release: 2016-11-25 13:53:46
Original
2607 people have browsed it

Suppose 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;​​

}​​​​​​​​​​​​​​​​​​​​


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

}

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