javascript - 在nodejs应用程序中读取res.json

标签 javascript json node.js

我有一个简单的nodejs get,运行如下:

app.get('/customer/:ent_cust_id', function (req, res, next) {
        var query = 'Select * from entcustinfo where ent_cust_id = ' + req.params.ent_cust_id;
        console.log('Select * from entcustinfo where ent_cust_id =' + req.params.ent_cust_id);
        client.execute(query, function (err, result) {
        if (err) return next (err);
        var row = result.rows[0];
            if (result.rows.length==0) {
                res.send('ent_cust_id: ' + req.params.ent_cust_id + ' not found. Not all data is loaded.');
        } else {
            var row = result.rows[0];
                //Response
                res.json({ent_cust_id: req.params.ent_cust_id, accts: row.get('accts'), offers: row.get('offers')});
        };
        });
 });

并将结果返回到数组中,如下所示:

{"ent_cust_id":"1013686356",
        "accts":{
            "3017":{"popn_typ_cd":"CNSMR_DIRCT_CHKG","prod_type_cd":"DDA","ent_prod_cd":"CNSDD","curr_amt":"14.67","bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0},
            "2752":{"popn_typ_cd":"CNSMR_LENDG","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":1},
            "888":{"popn_typ_cd":"CNSMR_DIRCT_MORT","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0}
            },
        "offers":{
            "5998":{"contacted":"Y","hit_home_date":"2015-06-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
            "6998":{"contacted":"Y","hit_home_date":"2015-07-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
            "7998":{"contacted":"Y","hit_home_date":"2015-08-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
            "506A":{"contacted":"Y","hit_home_date":"2015-06-18T04:00:00.000Z","creative":"Availability CIT","channel":"EM","campaign":"FUNDS_AVAILABILITY_CIT","campaign_tp":"NOTIFICATION"},
            "06_A":{"contacted":"Y","hit_home_date":"2015-06-29T04:00:00.000Z","creative":"Mob5X","channel":"EM","campaign":"EMAIL","campaign_tp":"UPSELL"},
            "5009":{"contacted":"Y","hit_home_date":"2015-06-05T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
            "6009":{"contacted":"Y","hit_home_date":"2015-07-09T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
            "7996":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
            "R_33S":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"},
            "R_34S":{"contacted":"Y","hit_home_date":"2025-12-31T05:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"},
            "R_22":{"contacted":"Y","hit_home_date":"2015-06-22T04:00:00.000Z","creative":"1Offer","channel":"EM","campaign":"TIERED CASH","campaign_tp":"SELL"}
            }
    }

我知道如何获取数组的第一层,但是如果我想获取数组的任何其他部分怎么办?

如果我还想检查 offers 中的 contacted 字段是否在任何结果中都标记为 Y,该怎么办?提供部分吗?

我该怎么做?

编辑:

看起来这只是一个字符串而不是一个数组。所以也许问题是,如何将其转换为数组和/或 json 对象?

最佳答案

首先,我没有看到任何数组,所以我对第一个问题有点困惑?

但在回答你的第二个问题时,如果你的数据已被评估为一个名为“data”的对象,该对象是上述 JSON 的数组,你可以这样做:

var custId = "1013686356";
var determineIfContacted = function(custId, data) {
    var customers = data.filter(function(item) { return item.ent_cust_id == custId; })
    var customer = customers[0]; // you should validate that you have a matching customer here
    var hasBeenContacted = false;
    foreach (var offerId in customer.offers) {
        if (customer.offers[offerId].contacted === "Y") {
            hasBeenContacted = true;
            break;
        }
    }
    return hasBeenContacted;
}

希望这有帮助(:

关于javascript - 在nodejs应用程序中读取res.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233976/

相关文章:

node.js - 在 node.js 中调用 https web 服务(在代理后面)

javascript - 用于创建模式对话框的 JQuery 编码可以在 jsfiddle 中工作,但不能在本地 html 文件中工作

javascript - D3 时区偏移量移至前一天

json - 缺少字段的 Spark JSON 中的 scala.MatchError : [abc, cde,null,3](属于 org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema)

javascript - 如何使用 jQuery 遍历 JSON 文件

mysql - 从 js 运行时插入错误,xampp shell 使用相同的 sql 工作正常

javascript - AngularJS ng-show 与 map 一起使用时不会触发

javascript - 当我打开日期时间选择器时,输入框中的时间会自动更改

php - 在 ListView 中显示从 .php 脚本接收到的数据

mysql - 如何使用 Express 显示来自 mysql 的图像