根据 ajax 请求访问 Json
access Json as result of ajax request
嗨,我该如何处理来自 ajax
的 json 回复
{"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
}
这样做我可以访问 suivi 和邮件
success: function(data){
$('#suivi').html(data.mail+" +");
$('#mail').html(data.suivi+" +");
}
但是我怎样才能得到档案清单(listSuivi)呢?
非常感谢
您可以使用 for 循环遍历这些项目。
有些选项是 for ... in 循环:
const data = {
"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
};
// For ... in loop
for(const key in data.listSuivi) {
const item = data.listSuivi[key];
console.log("Object: ", item);
console.log("Dossier:", item.dossier);
console.log("Refunds:", item.refunds_count);
}
或者for ... of loop in combination with Object.values
const data = {
"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
};
// For ... of loop with Object.values
for(const item of Object.values(data.listSuivi)) {
console.log("Object: ", item);
console.log("Dossier:", item.dossier);
console.log("Refunds:", item.refunds_count);
}
嗨,我该如何处理来自 ajax
的 json 回复{"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
}
这样做我可以访问 suivi 和邮件
success: function(data){
$('#suivi').html(data.mail+" +");
$('#mail').html(data.suivi+" +");
}
但是我怎样才能得到档案清单(listSuivi)呢? 非常感谢
您可以使用 for 循环遍历这些项目。
有些选项是 for ... in 循环:
const data = {
"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
};
// For ... in loop
for(const key in data.listSuivi) {
const item = data.listSuivi[key];
console.log("Object: ", item);
console.log("Dossier:", item.dossier);
console.log("Refunds:", item.refunds_count);
}
或者for ... of loop in combination with Object.values
const data = {
"suivi":5,
"mail":8,
"listSuivi":{
"0":{"dossier":"IWE123123","refunds_count":1},
"1":{"dossier":"IM00000009","refunds_count":2}
}
};
// For ... of loop with Object.values
for(const item of Object.values(data.listSuivi)) {
console.log("Object: ", item);
console.log("Dossier:", item.dossier);
console.log("Refunds:", item.refunds_count);
}