HI ,
var jsonObj = [] ;
for (var i = 0; i < 开发者_C百科data.jobs.length; i++) {
jsonObj.push({id: data.jobs[i].Dater, optionValue: data.jobs[i].INCPU});
}
alert(jsonObj);
I am getting as result as
[object Object],[object Object],[object Object]
That is because you are alerting an array. Trying alerting an individual index of that array.
alert(jsonObj[0])
If you want to produce a JSON serialisation, use a JSON parser library like json2.js.
The serialised form will also produce the expected result when you pass it to alert()
.
I believe you're trying to achieve this:
var obj = [];
// populate obj in a loop
var jsonStr = JSON.stringify(obj);
alert(jsonStr);
Live demo: http://jsfiddle.net/simevidas/Smd2P/1/
精彩评论