如何将 ResultSet 转换为 JSON 对象
how to convert ResultSet to JSON object
我们尝试使用以下代码将 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);
试试下面的代码:
var obj = {};
obj = [];
for(var i=0;i<result.rows.item.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);
输出:
[
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
}
]
如果您通过 obj.push(getAllCalelements)
向 obj
添加第二个对象,您将得到以下输出
[
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
},
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
}
]
@aschattney 根据您的答案即兴创作并使用 Object.keys()
使其动态化,并将其包装在一个函数中,以便您传递 resultSet
即在这种情况下,sqlResultSet
并且它 returns有效JSON.
function returnJsonFromSqlResultSet(sqlResultSet) {
var objArr = [];
if (sqlResultSet.rows.length > 0) {
var objKeys = Object.keys(sqlResultSet.rows.item(0)); //sample the keys from first element
for (var i = 0; i < sqlResultSet.rows.length; i++) {
var jsonObj = {}; // Object
for (var j = 0; j < objKeys.length; j++) {
jsonObj[objKeys[j]] = sqlResultSet.rows.item(i)[objKeys[j]];
}
objArr.push(jsonObj);
}
var json = JSON.stringify(objArr, undefined, 2);
return 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);
试试下面的代码:
var obj = {};
obj = [];
for(var i=0;i<result.rows.item.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);
输出:
[
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
}
]
如果您通过 obj.push(getAllCalelements)
向 obj
添加第二个对象,您将得到以下输出
[
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
},
{
"totalamount": "19760.22",
"discount": "7",
"NetAmount": "18377",
"vat": "8",
"GrossAmount": "8",
"bookOrder": "ABCANG00110"
}
]
@aschattney 根据您的答案即兴创作并使用 Object.keys()
使其动态化,并将其包装在一个函数中,以便您传递 resultSet
即在这种情况下,sqlResultSet
并且它 returns有效JSON.
function returnJsonFromSqlResultSet(sqlResultSet) {
var objArr = [];
if (sqlResultSet.rows.length > 0) {
var objKeys = Object.keys(sqlResultSet.rows.item(0)); //sample the keys from first element
for (var i = 0; i < sqlResultSet.rows.length; i++) {
var jsonObj = {}; // Object
for (var j = 0; j < objKeys.length; j++) {
jsonObj[objKeys[j]] = sqlResultSet.rows.item(i)[objKeys[j]];
}
objArr.push(jsonObj);
}
var json = JSON.stringify(objArr, undefined, 2);
return json;
}
}
谢谢!