简体   繁体   中英

javascript create json object from data table

There are two tables work plan and time breaks. I got problem with preparing the json data.

I want the json data look like below.

{"monday":{"start":"","end":"","breaks":[{"start":"","end":""},{"start":"","end":"0"}]},"tuesday":{"start":"","end":"","breaks":[{"start":"","end":""}}...

Table work plan and time breaks.

  $('#save-logic').click(function() { var TableDataWork = new Array(); var TableDataBreak = new Array(); var timeBreak = new Array(); var dayOfBreak={}; var dayOfWork ={}; //get data from work plan table $('.working-plan tbody tr').each(function(roww, trw){ dayOfWork = $(trw).find('td:eq(0)').text().replace(/\\s/g, ""); //get data from time breaks table $('.breaks tbody tr').each(function(rowb, trb){ dayOfBreak = $(trb).find('td:eq(0)').find('select').val(); timeBreak[rowb]={ 'start' : $(trb).find('td:eq(1)').find('input').val(), 'end' : $(trb).find('td:eq(2)').find('input').val(), }; TableDataWork[roww]={ dayOfWork: { 'start' : $(trw).find('td:eq(1)').find('input').val(), 'end' : $(trw).find('td:eq(2)').find('input').val(), 'breaks' : timeBreak, },}; }); }); TableDataWork.shift(); alert(TableDataWork.toSource()); }); 

I think var JSON.stringify() would be the option. Once you have created the TableDataWork.toSource() you could convert it to JSON by simply calling this function.

var myJSON = JSON.stringify(TableDataWork.toSource());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM