❶ 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');