我的数据库中有以下json 5次,我需要计算与no_of_steps
对应的asanas_id
总和。即,相同的json将循环5次,对于相应的no_of_steps
我需要总共asanas_id
。
{"yoga_prescription":[{"result":[{"asanas_sub_category_id":"7","asanas_id":"5","no_of_step":"45","ratio":"5"},{"asanas_sub_category_id":"8","asanas_id":"7","no_of_step":"5","ratio":"5"}],"asanas_category_id":"1"},{"result":[{"asanas_sub_category_id":"11","asanas_id":"13","no_of_step":"54","ratio":"55"}],"asanas_category_id":"2"},{"result":[{"asanas_sub_category_id":"15","asanas_id":"17","no_of_step":"5","ratio":"5"}],"asanas_category_id":"3"}]}
答案 0 :(得分:2)
我不太确定你在问什么,但是如果你的JSON中总是有相同的结构,那么你可以将数组循环到yoga_prescription
如果您提供更多信息,将会很有用。你使用哪种编程语言?
即。如果您使用的是javascript:
function sumJson (jsonVar) {
var jsonLength = jsonVar.yoga_prescription.length;
var jTotal = 0;
for (var i = 0; i < jsonLength; i++) {
var resultLength = JSONVAR.yoga_prescription[i].result.length;
for (var j = 0; j < resultLength; j++) {
jTotal += parseInt(JSONVAR.yoga_prescription[i].result[j].no_of_step);
}
}
return jTotal;
}
这里有一个工作示例:example