How does JS determine if json is null
- 2021-07-02 23:04:45
- OfStack
Let's not talk too much nonsense. Let's get to the point.
var jsonStr ={};
1. Judge whether json is empty or not
jQuery.isEmptyObject() ;
2. Judge whether the object is empty:
1. if (typeOf (x) = = "undefined")
2. if (typeOf (x)! = "object")
3. if (! x)
Among them, the third is the simplest method, but the third can't be judged by if (x), which is mutually exclusive, and can only be added in front of the object!
3. key of json cannot be repeated;
jsonStr [key] = "xxx", if it exists, it will be replaced, if it does not exist, it will be added.
4. Traverse json
for(var key in jsonStr){
alert(key+" "+jsonStr[key])
}