如何从html响应解析javascript数组(包含JSON对象作为元素)
How to parse a javascript array (containing JSON object as elements) from html response
我正试图编写一个nodejs脚本,从mongodb中的多个集合中获取数据。之后,必须将完整的数据写入一个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;
我在响应中看到的数据是:
[
[{
"_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对象?
JSON对象中的数组有一个函数getJSONObject(int index),您可以使用for loop:循环所有对象
var array;
for(int n = 0; n < array.length(); n++)
{
var object = array.getJSONObject(n);
// do some stuff....
}
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 用javascript从列表对象(JSON的)构建diffrent选项卡
- php请求带有多个对象json-jquery
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 动态属性对象 - JSON
- JavaScript 对象 (JSON) 中的动态设置值
- 从数组查询获取对象 JSON 值时出错
- 使用jQuerygetJSON将多维对象JSON转换为HTML
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 嵌套对象JSON排序JavaScript
- PHP在MySQL中保存来自Javascript的对象"JSON.stringify"通过Ajax
- 将JavaScript对象/ JSON转换为PHP数组
- JSON对象.JSON对象内部
- 访问对象的对象- JSON API
- 数组到对象JSON
- Message":"传入的无效对象(JSON数据格式化问题)