【判斷json是否為空】
var jsonStr ={};
1、判斷json是否為空
jQuery.isEmptyObject();
2、判斷對象是否為空:
1、if (typeof(x) == "undefined")
2、if (typeof(x) != "object")
3、if(!x)
其中第三種是最簡單的方法,但是第三種就不能用if(x)這種互斥的方法去判斷,只能在對象前面加!
3、json的key是不可以重復的;
jsonStr[key]="xxx",存在在替換,不存在則新增。
4、遍歷json
for(var key in jsonStr){
alert(key+" "+jsonStr[key])
}
【判斷返回是否json格式】
isJson = function(obj){ var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length; return isjson; } if (!isJson(data)) data = eval('('+data+')');//將字符串轉換成json格式