我需要你们的一些帮助/指导我如何才能获得json结果中的数据。我如何从product_quotas1&获取数据? product_quotas2? 如果有人可以帮助或指导我解决这个问题,我将不胜感激。
var myArrayNeed = {
"3": {
"id": "3",
"state": "1",
"charity_id": "3",
"unmet_quotas": {
"product_quotas1": {
"product_id": "134",
"quota": "32",
"remaining_quota": "32",
"progress": 0,
"vegetarian": "0"
},
"product_quotas2": {
"product_id": "153",
"quota": "12",
"remaining_quota": "12",
"progress": 0,
"vegetarian": "1"
}
}
}
}
jQuery.each(myArrayNeed, function(index, value) {
//console.log("index :" + index + "value :" + value);
jQuery.each(value, function(subindex, subvalue) {
// console.log("index :" + subindex + "value :" + subvalue);
var unmet_quotas = value.unmet_quotas;
//console.log('Unmet Quota : ' + unmet_quotas);
jQuery.each(unmet_quotas, function(subsubindex, subsubvalue) {
console.log("index :" , subsubindex , "value :" , subsubvalue);
});
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 0 :(得分:0)
稍微调整一下代码
var myArrayNeed={
"3": {
"id": "3",
"state": "1",
"charity_id": "3",
"unmet_quotas": {
"product_quotas1": {
"product_id": "134",
"quota": "32",
"remaining_quota": "32",
"progress": 0,
"vegetarian": "0"
},
"product_quotas2": {
"product_id": "153",
"quota": "12",
"remaining_quota": "12",
"progress": 0,
"vegetarian": "1"
}
}
}
}
jQuery.each(myArrayNeed, function(index, value) {
jQuery.each(value.unmet_quotas, function(subindex, subvalue) {
console.log(subvalue);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
第一个each
将遍历您的&#34;对象&#34;属性只是此示例的一个属性3
第二个each
将遍历unmet_quotas
的属性,这些属性是您想要的对象
答案 1 :(得分:0)
试试这段代码
var myArrayNeed = {
"3": {
"id": "3",
"state": "1",
"charity_id": "3",
"unmet_quotas": {
"product_quotas1": {
"product_id": "134",
"quota": "32",
"remaining_quota": "32",
"progress": 0,
"vegetarian": "0"
},
"product_quotas2": {
"product_id": "153",
"quota": "12",
"remaining_quota": "12",
"progress": 0,
"vegetarian": "1"
}
}
}
}
//console.log(myArrayNeed);
jQuery.each(myArrayNeed, function(index, value) {
jQuery.each(value.unmet_quotas, function(subindex, subvalue) {
jQuery.each(subvalue, function(subsubindex, subsubvalue) {
if (subsubindex == 'remaining_quota') {
console.log("index :", subsubindex, "value :", subsubvalue);
var remaining_quota = subsubvalue;
alert(remaining_quota);
}
});
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;