无法创建json

时间:2014-12-16 04:49:51

标签: javascript json

我将值存储在以下变量中并具有json结构。但是值并没有存储到json中。代码如下。

var ReportSelected =  [];
var PeopleSelected = [];
var FilterSelected = [];
var summaryJson = {
    "reportselectedjson" :  [],
    "PeopleSelected" : [],
    "FilterSelected" : []
};

for ( var int = 0; int < ReportSelected.length; int++) {
     summaryJson.reportselectedjson.push(ReportSelected[int]);
        }
 for ( var int2 = 0; int2 < PeopleSelected.length; int2++) {
    summaryJson.PeopleSelectedjson.push(PeopleSelected[int2]);
}
 for ( var int3 = 0; int3 < FilterSelected.length; int3++) {
    summaryJson.FilterSelectedjson.push(FilterSelected[int3]);

}
 console.log(JSON.stringify(summaryJson));

这在json中没有任何价值。

2 个答案:

答案 0 :(得分:0)

您的PeopleSelected,ReportSelected和FilterSelected数组都是空的。所以你不会在summaryJson中得到任何东西。

答案 1 :(得分:0)

是的,每个人都已经解决了这个问题。数组是空的......这是代码的略微编辑版本;

var ReportSelected = ['one','two','three'],
    PeopleSelected = ['test1','test2','test3'],
    FilterSelected = ['filt1','filt2','filt3'], 
    summaryJson = { 
        "Reportselectedjson" : [], 
        "PeopleSelectedjson" : [], 
        "FilterSelectedjson" : [] 
    };

for ( var int = 0; int < ReportSelected.length; int++) {
     summaryJson.Reportselectedjson.push(ReportSelected[int]);
}

for ( var int2 = 0; int2 < PeopleSelected.length; int2++) {
    summaryJson.PeopleSelectedjson.push(PeopleSelected[int2]);
}

for ( var int3 = 0; int3 < FilterSelected.length; int3++) {
    summaryJson.FilterSelectedjson.push(FilterSelected[int3]);

}

alert(JSON.stringify(summaryJson));

这是JSFiddle链接 - http://jsfiddle.net/3xajv687/