[英]How do I get specific value key pair in ajax javascript for datatype json?
我想從下面的代碼中的數據中獲取“狀態”的值,
$.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); }
});
以下是console.log結果的一部分:
鍵: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" , “指針”: “0”, “白板”: “0”, “mediaCoverage”: “0”, “停車”: “0”, “備注”: “”, “選擇器”: “2014年11月12日”}]}
我想要獲取“狀態”的值,即0,如上述結果所示,以便將其包括在for循環中(for(數據中的var鍵){...})以更改類'label-success '到'label-failure'(如果狀態為0)。您能幫我嗎?
你好,也許我錯了,但是
console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
當您說JSON.stringify(data);時,以字符串格式返回整個數據對象。 您需要在為數據提供特定鍵以從中讀取值時返回的值:
console.log('key: ' + key + '\n' + 'value: ' + data[key]);
編輯:我不確定data [key]是否會返回[object Object] ...,如果嘗試JSON.stringify(data [key])
我還建議您使用
for(var i = 0; i < data.length; i++){}
這使其可讀性強,並且被認為是提取數據最有效的方式。
編輯nr 2:
這是您的對象:
{"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"}]}
這有點嵌套,所以請嘗試大致了解您擁有的東西:
data = {
"2014-11-11": [],
"2014-11-12": []... }
該對象具有一個length方法,該方法返回對象的長度。 這允許您在Data Object上進行操作,將為您提供“ 2014-11-11”作為鍵,並使用此鍵可以訪問以下值:data [key]這將返回數組...從您的數組中讀取數據數組,您將不得不再次聲明data [key] [i] ...現在您可以像這樣讀取每個數組元素中的數據
data[key][i]["status"];
希望這可以以某種方式幫助...不能煩惱寫所有這些代碼: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));
}
},
請嘗試使用此代碼。
$.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.