❶ javascript/jquery如何判斷json數據中某深層的key是否存在
if(j&&j.input&&j.input.age){
alert("exist");
}
由於 null、NaN、0、undefined、空字復符串制 "" 都是可以被轉換成 false 的,所以要實現確定 age 屬性不是這些值,否則也會當做不存在。
補充,Object.hasOwnProperty 可以檢測對象是否擁有自定義(不搜索原型)屬性
if(j&&j.input&&j.input.hasOwnProperty("age")){
alert("exist");
}
❷ jquery驗證json里某個數據是否存在
vararr=[{
"name":"哈哈··",
"email":["123@qq.com","[email protected]"]
},{
"name":"呵呵··",
"email":["[email protected]","[email protected]"]
}];
vartmp="[email protected]";
for(vari=0;i<arr.length;i++){
varobj=arr[i];
if(obj.email.indexOf(tmp)!=-1){
alert("存在");
break;
}
}
❸ jquery解析json怎麼解析
獲取JSON數據,在jQuery中有一個簡單的方法 $.getJSON() 可以實現。
下面引用的是官方API對$.getJSON()的說明:
jQuery.getJSON( url, [data,] [success(data, textStatus, jqXHR)] )
urlA string containing the URL to which the request is sent.
dataA map or string that is sent to the server with the request.
success(data, textStatus, jqXHR)A callback function that is executed if the request succeeds.
回調函數中接受三個參數,第一個書返回的數據,第二個是狀態,第三個是jQuery的XMLHttpRequest,我們只使用到第一個參數。
$.each()是用來在回調函數中解析JSON數據的方法,下面是官方文檔:
jQuery.each( collection, callback(indexInArray, valueOfElement) )
collectionThe object or array to iterate over.
callback(indexInArray, valueOfElement)The function that will be executed on every object.
$.each()方法接受兩個參數,第一個是需要遍歷的對象集合(JSON對象集合),第二個是用來遍歷的方法,這個方法又接受兩個參數,第一個是遍歷的index,第二個是當前遍歷的值。哈哈,有了$.each()方法JSON的解析就迎刃而解咯。
functionloadInfo(){
$.getJSON("loadInfo",function(data){
$("#info").html("");//清空info內容
$.each(data.comments,function(i,item){
$("#info").append("<div>"+item.id+"</div>"+"<div>"+item.nickname+"</div>"+
"<div>"+item.content+"</div><hr/>");
});
});
}
❹ jQuery怎麼解析Json字元串
一、jQuery解析Json數據格式:
使用這種方法,必須在Ajax請求中設置參數:
1、dataType: "json"
獲取通過回調函數返回的數據並解析得到我們想要的值,看源碼:
jQuery.ajax({
url:full_url,
dataType:"json",
success:function(results){
alert(result.name);
}
});
jquery非同步請求將type(一般為這個配置屬性)設為「json」,或者利用$.getJSON()方法獲得伺服器返回,那麼就不需要eval()方法了,因為這時候得到的結果已經是json對象了,只需直接調用該對象即可,這里以$.getJSON方法為 例:
vardata="
{
root:
[
{name:'1',value:'0'},
{name:'6101',value:'北京市'},
{name:'6102',value:'天津市'},
{name:'6103',value:'上海市'},
{name:'6104',value:'重慶市'},
{name:'6105',value:'渭南市'},
{name:'6106',value:'延安市'},
{name:'6107',value:'漢中市'},
{name:'6108',value:'榆林市'},
{name:'6109',value:'安康市'},
{name:'6110',value:'商洛市'}
]
}";
$.getJSON("http://sanic.cnblogs.com/",{param:"sanic"},function(data){
//此處返回的data已經是json對象
//以下其他操作同第一種情況
$.each(data.root,function(idx,item){
if(idx==0){
returntrue;//同countinue,返回false同break
}
alert("name:"+item.name+",value:"+item.value);
});
});
二、jQuery解析Json對象:
jQuery提供了另一種方法「parseJSON」,這需要一個標準的JSON字元串,並返回生成的JavaScript對象。語法:
data = $.parseJSON(string);
看看它是如何運用的到實際開發中的:
jQuery.ajax({
url:dataURL,success:function(results){
varparsedJson=jQuery.parseJSON(results);
alert(parsedJson.name);
}
});
❺ 請教jquery高手,怎樣解析 json數據
這是jQuery里jQuery.post的定義:jQuery.post(url,[data],[callback],[type])
第四個參數可以指定返回的數據類型,可以設置成「json」。如果返回的內容符合json的格式,jQuery會自動進行解析的,然後你就可以使用了,比如:
$.post("../getData.aspx", function(data){
// 解析後,data是一個數組,數組里的元素是一個個對象
var item;
for(var i=0, len=data.length; i<len; i++) {
item=data[i];
alert(item.fieldName);
alert(item.filedValue);
}
}, 'json');