从节点js中的JSON对象获取数据?

时间:2017-02-08 06:45:46

标签: javascript json node.js

我试图通过传递字段名称从JSON对象中提取数据,但是当我尝试提取数据时,我得到了未定义的结果。出于某种原因,我获取JSON对象的方式不是我预期的方式。可能是什么错误?

JS代码 -

  function initfunc() {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "/dashboard",
                        data: JSON.stringify({country: country[counter], bankid: bankid[counter]}),
                        dataType: "json",
                        success: function (Result) {
                            Result = Result.Resultset
                            // console.log("result", Result);


                            //  console.log("result",Result);
                            var data = [];
                            var merchantname = [];
                            var successtranscs = [];
                            var failedtranscs = [];
                            var servicetranscs = [];
                            var bankname;
                            console.log("result",Result);
                            for(var i in Result) {
                                merchantname.push(Result[i].MERCHANTNAME); // or .MERCHANTNAME?
                                successtranscs.push(Result[i].NO_OF_SUCCESSFUL_TRANSACTIONS); // or .NO_OF_SUCCESSFUL_TRANSACTIONS?
                                failedtranscs.push(Result[i].NO_OF_FAILED_BANK_TRANSACTIONS); // or .NO_OF_FAILED_BANK_TRANSACTIONS?
                                servicetranscs.push(Result[i].NO_OF_FAILED_SERVICE_TRANSACTIONS); // or .NO_OF_FAILED_SERVICE_TRANSACTIONS?
                                bankname = Result[i].BankName; // or .BankName?
                            }
                            //console.log("merchantname",merchantname);
                            StackedChart(bankname, merchantname, successtranscs, failedtranscs, servicetranscs);
                            merchantname = [];
                            successtranscs = [];
                            failedtranscs = [];
                            servicetranscs = [];
                            rows = null;
                            if (counter == country.length - 1) {
                                counter = -1;
                                counter++;
                            }
                            else {
                                counter++;
                            }
                        }
                    });





                }

server.js -

app.post('/dashboard', function (req, res) {
    cnn.TransactionInfo(req.body.country,req.body.bankid, function (err, result) {
        var resultset = {"Result":result};
        console.log(resultset);

        res.send({ Resultset : resultset });


    });
});

存储过程的初始结果集 - 结果集 -

{ Result:
   [ { BankName: 'IM BANK',
       MERCHANTNAME: 'MPesa',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 29,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 4 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'Zuku',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 1,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'BALANCE',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 21,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'AIRTEL',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 2,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'SAFARICOM',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 4,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'FIXEDDEPOSIT',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 1,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'FOREX',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 1,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'KITSA2P',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 1,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'STATEMENT',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 2,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 },
     { BankName: 'IM BANK',
       MERCHANTNAME: 'TRANSFER',
       NO_OF_SUCCESSFUL_TRANSACTIONS: 1,
       NO_OF_FAILED_BANK_TRANSACTIONS: 0,
       NO_OF_FAILED_SERVICE_TRANSACTIONS: 0 } ] }

这是我将数据发送到客户端脚本后得到的结果 - 结果集变为 -

result:
Object
Result
:
Array[9]
0
:
Object
BankName
:
"IM BANK"
MERCHANTNAME
:
"MPesa"
NO_OF_FAILED_BANK_TRANSACTIONS
:
0
NO_OF_FAILED_SERVICE_TRANSACTIONS
:
2
NO_OF_SUCCESSFUL_TRANSACTIONS
:
25

2 个答案:

答案 0 :(得分:0)

您应该在向后端发送数据时在后端执行此操作

res.json(resultset);

在前端接收时,

console.log("result",JSON.stringify(Result)); //to display in string format

另外,您将获得所需格式的数据,以便根据需要将其推送到数组中

答案 1 :(得分:0)

USERS.JSON

{
   "user1" : {
      "name" : "mahesh",
      "password" : "password1",
      "profession" : "teacher",
      "id": 1
   },
   "user2" : {
      "name" : "suresh",
      "password" : "password2",
      "profession" : "librarian",
      "id": 2
   },
   "user3" : {
      "name" : "ramesh",
      "password" : "password3",
      "profession" : "clerk",       
      "id": 3
   }
}

代码

     var express = require('express');
            var app = express();
        var fs = require("fs");

    app.get('/listUsers', function (req, res) {
    fs.readFile(__dirname + "/" + "users.json", 'utf8', function (err, data) {


        console.log(data);
        res.end(data);
    });
})