编程语言
首页 > 编程语言> > javascript-如何将ResultSet转换为JSON对象

javascript-如何将ResultSet转换为JSON对象

作者:互联网

我们尝试使用以下代码将ResultSet转换为JSON对象:

var getAllCalelements = {};           // Object
    getAllCalelements['totalamount'] = [];          
    getAllCalelements['totalamount'].push(result.rows.item(i).totalamount);
    getAllCalelements['discount'] = [];          
    getAllCalelements['discount'].push(result.rows.item(i).invoicediscount);
    getAllCalelements['NetAmount'] = [];          
    getAllCalelements['NetAmount'].push(result.rows.item(i).namount);
    getAllCalelements['vat'] = [];          
    getAllCalelements['vat'].push(result.rows.item(i).vat);
    getAllCalelements['GrossAmount'] = [];          
    getAllCalelements['GrossAmount'].push(result.rows.item(i).gamount);
    getAllCalelements['bookOrder'] = [];          
    getAllCalelements['bookOrder'].push(result.rows.item(i).bookorder);

var json = JSON.stringify(getAllCalelements, undefined, 2);

但是,我们得到了这样的JSON

{
  "totalamount": [19760.22],
  "discount": [ 7],
  "NetAmount": [ 18377],
  "vat": [ 8],
  "GrossAmount": [ 8],
  "bookOrder": [ "ABCANG00110"]
}

而且,我们需要这样的JSON输出

[
    {
        "totalamount": 19760.22,
        "discount": 7,
        "NetAmount": 18377,
        "vat": 8,
        "GrossAmount": 8,
        "bookOrder": "ABCANG00555"
    }
]    

我已经尝试过,但是我没有得到如上所述的欲望输出,我正在得到JSON输出.但是我们需要以上格式的JSON.大家请引导我得到上面提到的我的愿望输出,谢谢.

解决方法:

var obj = [];

for (var i = 0; i < result.rows.length; i++){

    var getAllCalelements = {};           // Object
    getAllCalelements['totalamount'] = result.rows.item(i).totalamount ;
    getAllCalelements['discount'] = result.rows.item(i).invoicediscount;
    getAllCalelements['NetAmount'] = result.rows.item(i).namount;
    getAllCalelements['vat'] = result.rows.item(i).vat;
    getAllCalelements['GrossAmount'] = result.rows.item(i).gamount;
    getAllCalelements['bookOrder'] = result.rows.item(i).bookorder;
    obj.push(getAllCalelements);

}

var json = JSON.stringify(obj, undefined, 2); 

标签:cordova,jquery-mobile,resultset,json,javascript
来源: https://codeday.me/bug/20191120/2047282.html