admin管理员组文章数量:1312717
Im creating an invoice for books, and aim to submit it via ajax. Im trying to json encode the array of books in the invoice, however I keep getting a blank value
//create item list
var order_items = [];
$('#mi_books tbody tr.userbooks').each(function(index)
{
var bookisbn = $(this).find('td .mi_isbn').text();
var bookdata = [];
bookdata['isbn'] = bookisbn;
bookdata['title'] = $(this).find('.mi_title').text();
bookdata['qty'] = $(this).find('.mi_qty').text();
bookdata['price'] = $(this).find('.mi_price').text();
order_items.push(bookdata);
});
alert(JSON.stringify(order_items));
alert(order_items.toString());
console.log(order_items);
alert(JSON.stringify(order_items));
Outputs: [[]]
alert(order_items.toString());
Outputs: blank
console.log(order_items);
Output:
Array[1]
0: Array[0]
isbn: "9781401216672"
length: 0
price: "1007"
qty: "1"
title: "Batman: The Killing Joke"
__proto__: Array[0]
length: 1
__proto__: Array[0]
My array is being created, but somehow I cant seem to json encode it? Am I doing something wrong?
Im creating an invoice for books, and aim to submit it via ajax. Im trying to json encode the array of books in the invoice, however I keep getting a blank value
//create item list
var order_items = [];
$('#mi_books tbody tr.userbooks').each(function(index)
{
var bookisbn = $(this).find('td .mi_isbn').text();
var bookdata = [];
bookdata['isbn'] = bookisbn;
bookdata['title'] = $(this).find('.mi_title').text();
bookdata['qty'] = $(this).find('.mi_qty').text();
bookdata['price'] = $(this).find('.mi_price').text();
order_items.push(bookdata);
});
alert(JSON.stringify(order_items));
alert(order_items.toString());
console.log(order_items);
alert(JSON.stringify(order_items));
Outputs: [[]]
alert(order_items.toString());
Outputs: blank
console.log(order_items);
Output:
Array[1]
0: Array[0]
isbn: "9781401216672"
length: 0
price: "1007"
qty: "1"
title: "Batman: The Killing Joke"
__proto__: Array[0]
length: 1
__proto__: Array[0]
My array is being created, but somehow I cant seem to json encode it? Am I doing something wrong?
Share Improve this question edited Jul 10, 2013 at 14:23 Antony 15.1k10 gold badges47 silver badges76 bronze badges asked Jul 10, 2013 at 14:14 pinkpixycoderpinkpixycoder 892 silver badges6 bronze badges3 Answers
Reset to default 4Array
and Object
are different beasts. Your bookdata
is not an array, but an object, thus, you should create it with
var bookdata = {};
Arrays are serialized differently with JSON.stringify()
as opposed to regular objects (only properties of UInt32 are serialized). Since you're only adding textual properties to the bookdata
, you should use anonymous objects like this:
var bookdata = {
isbn: bookisbn,
title: $(this).find('.mi_title').text(),
qty: $(this).find('.mi_qty').text(),
price: $(this).find('.mi_price').text()
};
you may try
var order_items = {};
$('#mi_books tbody tr.userbooks').each(function(index)
{
var bookisbn = $(this).find('td .mi_isbn').text();
var bookdata = {
'isbn': bookisbn,
'title': $(this).find('.mi_title').text(),
'qty': $(this).find('.mi_qty').text(),
'price': $(this).find('.mi_price').text()
};
order_items[index] = bookdata;
});
alert(JSON.stringify(order_items));
your only mistake was a try to create associative arrays instead of using objects, that can do it
本文标签: jqueryStringify a multidimension array with javascriptStack Overflow
版权声明:本文标题:jquery - Stringify a multi-dimension array with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741902623a2403953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论