1

我有一个我想在这里做的工作测试示例:http: //jsfiddle.net/2BxVk/6/

我现在用(像这样)填充了名为 allMonths 的可观察数组,而不是测试月份

self.tak = ko.observable(100);
self.styckpris = ko.observable(10);
self.grundpris = ko.observable(500);

self.allMonths = ko.observableArray([
new monthData(2013, 1, 412, 142, self),
new monthData(2013, 2, 112, 642, self),              
new monthData(2013, 2, 100, 742, self),
new monthData(2013, 3, 6513, 69, self),
new monthData(2013, 4, 34, 211, self),
new monthData(2013, 5, 123, 435, self),
new monthData(2013, 6, 412, 142, self),
new monthData(2013, 7, 412, 142, self)
]);

我想做一个 $.getJSON 并将其插入到我的 obs 数组中。

如果我直接从 url 查看它,这是 $.getJSON 源的输出:

[{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":2,"ss":784,"ms":576,"count":1360,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":3,"ss":977,"ms":636,"count":1613,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":4,"ss":1040,"ms":726,"count":1766,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":5,"ss":1373,"ms":1013,"count":2386,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":6,"ss":856,"ms":612,"count":1468,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":7,"ss":594,"ms":299,"count":893,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":8,"ss":1261,"ms":826,"count":2087,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":9,"ss":1092,"ms":729,"count":1821,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":10,"ss":1097,"ms":747,"count":1844,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":11,"ss":872,"ms":706,"count":1578,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":12,"ss":329,"ms":110,"count":439,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":2,"ss":911,"ms":0,"count":911,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":3,"ss":1002,"ms":0,"count":1002,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":4,"ss":1157,"ms":0,"count":1157,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":5,"ss":852,"ms":421,"count":1273,"price":20000}]

我一直在尝试遍历从 getJSON 获得的这个数组并将它们放在 allMonths 中,但我无法找出正确的语法。我对 tak、styckpris 或 grundpris,前 3 个属性不感兴趣,我想要年、月、ss 和 ms

我想要类似的东西:

var allMonths = ko.observableArray();
$.getJSON('Simulera/monthData', function (data) {
            $.each(data, function (i, val) {
                allMonths.push( new monthData(val.year, val.month, val.ss, val.ms, self) );
            });
        });

但这不起作用。如何查看 json 返回的信息并将它们作为 monthData 对象类型插入到我的可观察数组中?

4

1 回答 1

3

尝试添加self.

$.each(data, function (i, val) {
    self.allMonths.push( new monthData(val.year, val.month, val.ss, val.ms, self) );
});

你的小提琴的工作版本在这里

于 2013-05-21T08:33:17.323 回答