I am trying to send the following data structure to the Server using Json:
我正在尝试使用Json将以下数据结构发送到服务器:
{"1":{"9":["Foto","45", "39"],"24":["Video","34", "8"]},
"2":{"45":["Camara","3", "40"],"7":["Video","96", "7"]}}
Using the push function like this:
使用如下的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;
But I will like to create an associative Array like this:
但我想创建一个这样的关联数组:
Because I don't know how to split this data structure to get each value of the variable Array a
.
因为我不知道如何分割数据结构来得到变量数组a的每个值。
For that reason I will like to create an associative Array where I can see which data is what and send it to the Server.
出于这个原因,我想创建一个关联数组,在这里我可以看到哪个数据是什么,并将其发送到服务器。
1 个解决方案
#1
0
i don't know if i got you right... but you have a table, containing table rows with the class data. you want to loop through those rows and create a json to send to the server ...
我不知道我是不是把你弄对了…但是您有一个表,其中包含有类数据的表行。您希望循环遍历这些行,并创建一个json发送到服务器…
if that's the case - here we go:
如果是这样的话,我们开始:
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
see working fiddle here: http://jsfiddle.net/mdke87k1/
请参阅下面的工作小提琴:http://jsfiddle.net/mdke87k1/
i've included an 'output div' where i put in the resulting json. as far as i can see it - it differs from yours, since the outer bracket is missing. but i guess this should help to get you started.
我已经包含了一个'output div',在其中输入结果json。在我看来,它和你的不一样,因为外面的括号不见了。但我想这应该能帮助你开始。
#1
0
i don't know if i got you right... but you have a table, containing table rows with the class data. you want to loop through those rows and create a json to send to the server ...
我不知道我是不是把你弄对了…但是您有一个表,其中包含有类数据的表行。您希望循环遍历这些行,并创建一个json发送到服务器…
if that's the case - here we go:
如果是这样的话,我们开始:
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
see working fiddle here: http://jsfiddle.net/mdke87k1/
请参阅下面的工作小提琴:http://jsfiddle.net/mdke87k1/
i've included an 'output div' where i put in the resulting json. as far as i can see it - it differs from yours, since the outer bracket is missing. but i guess this should help to get you started.
我已经包含了一个'output div',在其中输入结果json。在我看来,它和你的不一样,因为外面的括号不见了。但我想这应该能帮助你开始。