【问题标题】:How to parse a javascript array (containing JSON object as elements) from html response如何从 html 响应中解析 javascript 数组(包含 JSON 对象作为元素)
【发布时间】:2016-07-12 06:49:04
【问题描述】:

我正在尝试编写一个从 mongodb 中的多个集合中获取数据的 nodejs 脚本。之后,必须将完整的数据写入一个 json 响应。 目前我采用的方法是将每个集合中的 json 数据推送到一个数组中,然后将该数组作为响应发送。但在客户端脚本中我无法读取数据。 我为写响应而编写的代码(Router.js)是:

appTableProdSchema.js

var conn = require('mongoose');

module.exports = conn.model('AppTableProd', {
    snapShotTime : String,
    nfs: Array,    
    hive: Array,
    beeline: Array,
    hs2: Array,
    beeswax: Array,
    df: Array 
});

hs2_10010Schema.js

var conn = require('mongoose');

module.exports = conn.model('hiveserver2_10010', {
    snapShotTime : String,
    hs2: Array
});

Router.js

var moment = require('moment'); //add moment

var records;
//mongoose schema
var appTableProdSchema = require("./appTableProdSchema.js");
var hs2 = require("./hs2_10010Schema.js");

router.get('/getHealthReport', function(req, res) {
    // Return the most recent document
    var fullData = [];
    records = appTableProdSchema
                   .find()
                   .sort({'_id': -1})
                   .limit(1)
                   .exec(function(err, data) {
                        if (err) return res.sendStatus(500);
                        fullData.push(data);
                   });


    records = hs2
               .find()
               .sort({'_id': -1})
               .limit(1)
               .exec(function(err, data) {
                    if (err) return res.sendStatus(500);
                    fullData.push(data);
                    res.json(fullData);
               }); 

    }); 

module.exports = router;

我在 Response 中看到的数据是:

[
    [{
        "_id":"56f178cf64dadf6411ccc3fa",
        "snapShotTime":"2016-03-22-16:50:05",
        "__v":0,
        "df":["data_node_1","data_node_2","data_node_3","data_node_4","data_node_5","data_node_13","data_node_10","data_node_9","data_node_6","data_node_8","data_node_12","data_node_7","data_node_11","ebdp-ch2-d146p.sys.comcast.net","ebdp-ch2-d192p.sys.comcast.net"],
        "beeswax":["data_node_10","data_node_1","data_node_11","data_node_13","data_node_4","data_node_2","data_node_3","data_node_6","data_node_12","data_node_5","data_node_7","data_node_8"],
        "hs2":["data_node_7","data_node_1","data_node_10","data_node_13","data_node_4","data_node_3","data_node_2","data_node_9","data_node_11","data_node_5","data_node_8","data_node_6","data_node_12"],
        "beeline":["data_node_10","data_node_1","data_node_11","data_node_13","data_node_4","data_node_2","data_node_3","data_node_6","data_node_12","data_node_5","data_node_7","data_node_8"],
        "hive":["data_node_4","data_node_2","data_node_3","data_node_7","data_node_8","data_node_5","data_node_1","data_node_6","data_node_12","data_node_11","data_node_13","data_node_9","data_node_10"],
        "nfs":["data_node_7","data_node_4","data_node_1","data_node_3","data_node_10","data_node_5","data_node_13","data_node_9","data_node_11","data_node_6","data_node_8","data_node_12","data_node_2"]
        }],

    [{
        "_id":"56f178cf64dadf6411ccc3fb",
        "snapShotTime":"2016-03-22 16:50:05",
        "__v":0,
        "hs2":["data_node_9"]
    }]
]

我如何编码以从数组中获取 json 对象?

【问题讨论】:

    标签: javascript jquery json ajax node.js


    【解决方案1】:

    JSON 对象中的数组有一个函数 getJSONObject(int index),你可以使用 for-loop 循环遍历所有对象:

    var array;
    for(int n = 0; n < array.length(); n++)
       {
           var object = array.getJSONObject(n);
           // do some stuff....
       }
    

    【讨论】:

    • 我猜在 javascript 脚本中我不能使用 JSONArray 和 JSONObject。这是在 java 中完成的。
    • 它应该可以工作。如果没有,那么您可以创建一个自定义迭代器。然后你应该可以通过 json 数组。你可以看看:developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
    • 请使用正确的 javascript 语法,因为这是 javascript 中的语法错误。 Javascript 不支持用类型声明变量。如果您正在尝试这样做,您可以改为使用 var array;var object = array.getJSONObject(n);
    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多