我试图使用Json向服务器发送以下数据结构:
{"1":{"9":["Foto","45", "39"],"24":["Video","34", "8"]},
"2":{"45":["Camara","3", "40"],"7":["Video","96", "7"]}}使用如下的push函数:
var a = {};
var b = {};
var key
$("table tr.data").each(function(i) {
var row = [];
key = i;
row.push($(this).find('td').eq(1).text());
row.push($(this).find('td').eq(2).text());
row.push($(this).find('td').eq(3).text());
b[key] = row;
});
a[id_valor] = b;但是我想创建这样一个关联数组:
因为我不知道如何分割这个数据结构来获取变量Array a的每个值。
由于这个原因,我想创建一个关联数组,在这里我可以看到哪些数据是什么,并将其发送到服务器。
发布于 2015-09-04 11:39:31
我不知道我对不对.但是您有一个表,其中包含带有类数据的表行。您希望遍历这些行并创建一个json发送到服务器..。
如果是这样的话-我们走吧:
var myTable = {}; //declare a table object
$(".data").each(function(index) { //loop through each .data row, passing the current row index into the function
var tableRow = []; //create row array for current row
$(this).find("td").each(function (){ // loop through every table cell
tableRow.push($(this).text()); //add the content of the td to the tablerow
});
myTable[index] = tableRow; //add the table row array to the table object with 'index' as key
});
var myString = JSON.stringify(myTable); //create JSON out of table object见此处工作小提琴:http://jsfiddle.net/mdke87k1/
我已经包含了一个“输出div”,我在其中输入了生成的json。据我所知,它与你的不同,因为外面的支架不见了。但我想这应该能让你开始工作。
https://stackoverflow.com/questions/32396097
复制相似问题