0

我需要从 JSON 数据中获取每列的总数。下面是我的 JSON 数据,我想得到每列的总计。

[
 {"campaign":"MD001","seen":650,"taken":62,"eligible":542,"total":1000,"ind":"E"},
 {"campaign":"MD002","seen":550,"taken":52,"eligible":342,"total":1000,"ind":"E"},
 {"campaign":"MD003","seen":800,"taken":69,"eligible":642,"total":1000,"ind":"W"},
 {"campaign":"MD004","seen":500,"taken":62,"eligible":527,"total":1000,"ind":"E"},
 {"campaign":"MD005","seen":350,"taken":29,"eligible":992,"total":1000,"ind":"w"},
 {"campaign":"MD006","seen":550,"taken":52,"eligible":342,"total":1000,"ind":"W"}
]

上面的意思是“合格”,每个“合格”都有不同的价值,所以我需要从上面的数据中得到一个“合格”的总数。

4

2 回答 2

4

那么你必须计算它:

var data = // your json data
var total = 0;
for(var i = 0; i < data.length; i++){
    total += parseInt(data[i].eligible);        
}

这是一个小提琴http://jsfiddle.net/CVpxR/

于 2013-08-14T10:36:28.827 回答
2

你可以这样做:

data = [
    {"campaign":"MD001","seen":650,"taken":62,"eligible":542,"total":1000,"ind":"E"},
    {"campaign":"MD002","seen":550,"taken":52,"eligible":342,"total":1000,"ind":"E"},
    {"campaign":"MD003","seen":800,"taken":69,"eligible":642,"total":1000,"ind":"W"},
    {"campaign":"MD004","seen":500,"taken":62,"eligible":527,"total":1000,"ind":"E"},
    {"campaign":"MD005","seen":350,"taken":29,"eligible":992,"total":1000,"ind":"w"},
    {"campaign":"MD006","seen":550,"taken":52,"eligible":342,"total":1000,"ind":"W"}
]

var total = 0;

$.each(data, function(index, item){
    total = total + item.eligible;
});

工作小提琴:http: //jsfiddle.net/2A9qv/2/

于 2013-08-14T10:33:10.300 回答