如何将ResultSet转换为JSON对象

时间:2022-01-30 12:02:39

We have tried to convert ResultSet into JSON object using following code :

我们尝试使用以下代码将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);

But, We got JSON like this

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

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

And, We need JSON output like this

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

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

I have tried but i am not getting desire output as mention above, I am getting JSON output. But We need JSON in the above format. All please guide me to get my desire output which is mention above, Thanks.

我已经尝试但是我没有得到上面提到的欲望输出,我得到JSON输出。但我们需要以上述格式的JSON。所有请指导我获得上面提到的我的欲望输出,谢谢。

3 个解决方案

#1


2  

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); 

#2


1  

Try below code :

试试以下代码:

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);  

Output :

输出:

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

If you are adding second object to obj by obj.push(getAllCalelements) you will get below output

如果你通过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"
  }
]

#3


0  

@aschattney Improvised on your answer and made it dynamic with Object.keys() also wrapped it in a function so you pass the resultSet i.e. in this case, sqlResultSet and it returns valid JSON.

@aschattney在你的答案上进行了简化,并使用Object.keys()将其包装在一个函数中,因此你传递了res​​ultSet,即在这种情况下,sqlResultSet并返回有效的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;

    }
}

Thanks!

谢谢!

#1


2  

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); 

#2


1  

Try below code :

试试以下代码:

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);  

Output :

输出:

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

If you are adding second object to obj by obj.push(getAllCalelements) you will get below output

如果你通过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"
  }
]

#3


0  

@aschattney Improvised on your answer and made it dynamic with Object.keys() also wrapped it in a function so you pass the resultSet i.e. in this case, sqlResultSet and it returns valid JSON.

@aschattney在你的答案上进行了简化,并使用Object.keys()将其包装在一个函数中,因此你传递了res​​ultSet,即在这种情况下,sqlResultSet并返回有效的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;

    }
}

Thanks!

谢谢!