用javascript Stringify一个多维数组。

时间:2021-12-10 21:36:08

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

我正在为图书创建一个发票,目标是通过ajax提交。我试图在发票中对图书数组进行json编码,但是我总是得到一个空值

 //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: [[]]

警报(JSON.stringify(order_items));输出:[[]]

alert(order_items.toString());
Outputs: blank

警报(order_items.toString());输出:空白

console.log(order_items);
Output:

console.log(order_items);输出:

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?

我的数组正在被创建,但不知何故,我似乎无法对它进行json编码?我做错什么了吗?

3 个解决方案

#1


0  

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

您唯一的错误是尝试创建关联数组,而不是使用对象

#2


4  

Array and Object are different beasts. Your bookdata is not an array, but an object, thus, you should create it with

数组和对象是不同的东西。你的bookdata不是一个数组,而是一个对象,因此,你应该用它来创建它

var bookdata = {};

var bookdata = { };

#3


3  

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:

使用JSON.stringify()序列化数组与使用常规对象不同(只有UInt32的属性是序列化的)。由于您只向bookdata添加文本属性,所以应该使用以下匿名对象:

var bookdata = {
    isbn: bookisbn,
    title: $(this).find('.mi_title').text(),
    qty: $(this).find('.mi_qty').text(),
    price: $(this).find('.mi_price').text()
};

#1


0  

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

您唯一的错误是尝试创建关联数组,而不是使用对象

#2


4  

Array and Object are different beasts. Your bookdata is not an array, but an object, thus, you should create it with

数组和对象是不同的东西。你的bookdata不是一个数组,而是一个对象,因此,你应该用它来创建它

var bookdata = {};

var bookdata = { };

#3


3  

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:

使用JSON.stringify()序列化数组与使用常规对象不同(只有UInt32的属性是序列化的)。由于您只向bookdata添加文本属性,所以应该使用以下匿名对象:

var bookdata = {
    isbn: bookisbn,
    title: $(this).find('.mi_title').text(),
    qty: $(this).find('.mi_qty').text(),
    price: $(this).find('.mi_price').text()
};