[英]How do I get specific value key pair in ajax javascript for datatype json?
I want to get the value of "status" from data in the code below, 我想从下面的代码中的数据中获取“状态”的值,
$.ajax({
dataType: "json",
url: "calendar/php/date.php",
type: "POST",
data: {
select: "%Y-%c-%e",
where: "%Y-%c",
d: y + "-" + m,
order: "timestamp, id"
},
beforeSend: function() { $('#loading').show(); },
success: function(data) {
sessionStorage[y+"-"+m] = JSON.stringify(data);
for (var key in data) {
$("<span class=\"label label-success\">" + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
}
},
complete: function() { $('#loading').fadeOut(200); }
});
Following is some part of the console.log result: 以下是console.log结果的一部分:
key: 2014-11-11 value: {"2014-11-11":[{"0":"3","1":"2014-11-11 11:11:00","2":"2014-11-28 10:12:00","3":"test","4":"test","5":"0","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-11","id":"3","timestamp":"2014-11-11 11:11:00","toTimestamp":"2014-11-28 10:12:00","title":"test","location":"test","status":"0","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-11"}],"2014-11-12":[{"0":"15","1":"2014-11-12 07:07:00","2":"2014-11-12 03:09:00","3":"test","4":"test","5":"1","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-12","id":"15","timestamp":"2014-11-12 07:07:00","toTimestamp":"2014-11-12 03:09:00","title":"test","location":"test","status":"1","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speake
键:2014-11-11值:{“ 2014-11-11”:[{“ 0”:“ 3”,“ 1”:“ 2014-11-11 11:11:00”,“ 2”:“ 2014-11-28 10:12:00“,” 3“:”测试“,” 4“:”测试“,” 5“:” 0“,” 6“:”“,” 7“:”“, “8”: “”, “9”: “0”, “10”: “0”, “11”: “0”, “12”: “0”, “13”: “0”, “14” : “0”, “15”: “0”, “16”: “”, “17”: “2014年11月11日”, “ID”: “3”, “时间戳”:“2014年11月11日11:11:00“,” toTimestamp“:” 2014-11-28 10:12:00“,” title“:” test“,” location“:” test“,” status“:” 0“,” organizer “:””, “organizerContact”: “”, “organizerEmail”: “”, “投影”: “0”, “膝上型计算机”: “0”, “扬声器”: “0”, “指针”: “0” , “白板”: “0”, “mediaCoverage”: “0”, “停车”: “0”, “备注”: “”, “选择器”: “2014年11月11日”}],“2014-11 -12“:[{” 0“:” 15“,” 1“:” 2014-11-12 07:07:00“,” 2“:” 2014-11-12 03:09:00“,” 3 “:” 测试”, “4”: “测试”, “5”: “1”, “6”: “”, “7”: “”, “8”: “”, “9”: “0” , “10”: “0”, “11”: “0”, “12”: “0”, “13”: “0”, “14”: “0”, “15”: “0”,” 16“:”“,” 17“:” 2014-11-12“,” id“:” 15“,” timestamp“:” 2014-11-12 07:07:00“,” toTimestamp“:” 2014- 11-12 03:09:00“,” title“:” test“,” location“:” test“,” status“:” 1“,” organizer“:”“,” organizerContact“:”“,” organizerEmail“ “:” “ ”投影“: ”0“, ”膝上型计算机“: ”0“,” SPEAKE r":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-12"}]}
R “:” 0" , “指针”: “0”, “白板”: “0”, “mediaCoverage”: “0”, “停车”: “0”, “备注”: “”, “选择器”: “2014年11月12日”}]}
I want get the value of "status" ie 0, as seen in the above result, in order to include it in the for loop (for (var key in data) {...}) to change the class 'label-success' to 'label-failure' if the status is 0. Could you help me? 我想要获取“状态”的值,即0,如上述结果所示,以便将其包括在for循环中(for(数据中的var键){...})以更改类'label-success '到'label-failure'(如果状态为0)。您能帮我吗?
Hello maybe I am wrong but 你好,也许我错了,但是
console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
returns the whole data object in string format when you say JSON.stringify(data); 当您说JSON.stringify(data);时,以字符串格式返回整个数据对象。 You want the value which is returned when you give data a specific key to read values from:
您需要在为数据提供特定键以从中读取值时返回的值:
console.log('key: ' + key + '\n' + 'value: ' + data[key]);
EDIT: Im not sure if data[key] will return a [object Object]... if it does try JSON.stringify(data[key]) 编辑:我不确定data [key]是否会返回[object Object] ...,如果尝试JSON.stringify(data [key])
I would also suggest Itterating through data with the 我还建议您使用
for(var i = 0; i < data.length; i++){}
this makes it readable and is measured the most performant way of extracting data. 这使其可读性强,并且被认为是提取数据最有效的方式。
EDIT nr 2: 编辑nr 2:
This is your object: 这是您的对象:
{"2014-11-11":[{"0":"3","1":"2014-11-11 11:11:00","2":"2014-11-28 10:12:00","3":"test","4":"test","5":"0","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-11","id":"3","timestamp":"2014-11-11 11:11:00","toTimestamp":"2014-11-28 10:12:00","title":"test","location":"test","status":"0","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-11"}],"2014-11-12":[{"0":"15","1":"2014-11-12 07:07:00","2":"2014-11-12 03:09:00","3":"test","4":"test","5":"1","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-12","id":"15","timestamp":"2014-11-12 07:07:00","toTimestamp":"2014-11-12 03:09:00","title":"test","location":"test","status":"1","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-12"}]}
this is a bit nested, so try to get an overview of what you have: 这有点嵌套,所以请尝试大致了解您拥有的东西:
data = {
"2014-11-11": [],
"2014-11-12": []... }
This object has a length method that returns the length of your object. 该对象具有一个length方法,该方法返回对象的长度。 this allows you to itterate over the Data Object will give you "2014-11-11" as key and with this key you can access your values like this: data[key] this will return your array...to read data from your array you will have to itterate again data[key][i]... now you can read the data inside each array element like this
这允许您在Data Object上进行操作,将为您提供“ 2014-11-11”作为键,并使用此键可以访问以下值:data [key]这将返回数组...从您的数组中读取数据数组,您将不得不再次声明data [key] [i] ...现在您可以像这样读取每个数组元素中的数据
data[key][i]["status"];
Hope this helped somehow... cant be bothered to write all this code :D 希望这可以以某种方式帮助...不能烦恼写所有这些代码:D
success: function(data) {
sessionStorage[y+"-"+m] = JSON.stringify(data);
for (var key in data) {
var status = data['status'];
var klass = status === 0 ? 'label-failure' : 'label-success';
$('<span class="label '+klass+'">' + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
}
},
Try this code instead. 请尝试使用此代码。
$.ajax({
dataType: "json",
url: "calendar/php/date.php",
type: "POST",
data: {
select: "%Y-%c-%e",
where: "%Y-%c",
d: y + "-" + m,
order: "timestamp, id"
},
beforeSend: function() { $('#loading').show(); },
success: function(data) {
sessionStorage[y+"-"+m] = JSON.stringify(data);
for (var key in data) {
for (var i in data[key]) {
$("<span class=\"label " + ((data[key][i] === "0") ? "label-failure" : "label-success") + "\">" + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
}
console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
}
},
complete: function() { $('#loading').fadeOut(200); }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.