使用node.js解析json数组
我是node.js新手,如果这是一件非常简单的事情,我深表歉意 我有以下节点js脚本:使用node.js解析json数组,json,node.js,rest,Json,Node.js,Rest,我是node.js新手,如果这是一件非常简单的事情,我深表歉意 我有以下节点js脚本: var http = require("http"); var bodyParser = require("body-parser"); var options = { "method" : "GET", "hostname" : "xxx.xxx.xxx.xxx", "port" : "18080", "path" : "/api/v1/applications/app-
var http = require("http");
var bodyParser = require("body-parser");
var options = {
"method" : "GET",
"hostname" : "xxx.xxx.xxx.xxx",
"port" : "18080",
"path" : "/api/v1/applications/app-20180103124606-0007/stages/0"
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = JSON.parse(Buffer.concat(chunks));
console.log(body);
});
});
req.end();
它返回以下JSON:
[ { status: 'COMPLETE',
stageId: 0,
attemptId: 0,
numActiveTasks: 0,
numCompleteTasks: 1,
numFailedTasks: 0,
executorRunTime: 2738,
executorCpuTime: 1207164005,
submissionTime: '2018-01-03T12:46:10.796GMT',
firstTaskLaunchedTime: '2018-01-03T12:46:10.810GMT',
completionTime: '2018-01-03T12:46:14.513GMT',
inputBytes: 0,
inputRecords: 99171,
outputBytes: 0,
outputRecords: 0,
shuffleReadBytes: 0,
shuffleReadRecords: 0,
shuffleWriteBytes: 1468516,
shuffleWriteRecords: 3872,
memoryBytesSpilled: 0,
diskBytesSpilled: 0,
name: 'reduceByKey at /scripts/wordcount.py:37',
details: 'org.apache.spark.rdd.RDD.<init>(RDD.scala:104)\norg.apache.spark.api.python.PairwiseRDD.<init>(PythonRDD.scala:391)\nsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\nsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\nsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\njava.lang.reflect.Constructor.newInstance(Constructor.java:423)\npy4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)\npy4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)\npy4j.Gateway.invoke(Gateway.java:236)\npy4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)\npy4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)\npy4j.GatewayConnection.run(GatewayConnection.java:214)\njava.lang.Thread.run(Thread.java:748)',
schedulingPool: 'default',
accumulatorUpdates:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
tasks: { '0': [Object] },
executorSummary: { '0': [Object] } } ]
[{状态:“完成”,
阶段ID:0,
attemptId:0,
numActiveTasks:0,
numCompleteTasks:1,
numFailedTasks:0,
电话:2738,
执行人时间:1207164005,
提交时间:“2018-01-03T12:46:10.796GMT”,
firstTaskLaunchedTime:'2018-01-03T12:46:10.810GMT',
完工时间:“2018-01-03T12:46:14.513GMT”,
输入字节:0,
输入记录:99171,
输出字节:0,
输出记录:0,
shuffleReadBytes:0,
随机记录:0,
shuffleWriteBytes:1468516,
ShuffleWriterRecords:3872,
已清除的MemoryBytes:0,
diskBytesSpilled:0,
名称:'reduceByKey at/scripts/wordcount.py:37',
详细信息:“org.apache.spark.rdd.rdd.(rdd.scala:104)\norg.apache.spark.api.python.PairwiseRDD.(PythonRDD.scala:391)\nsun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)\nsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\nsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\njava.lang.reflect.Constructor.newInstance(Constructor.java:423)\npy4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)\npy4j.ReflectionEngine.invoke(ReflectionEngine.java:357)\npy4j.Gateway.invoke(Gateway.java:236)\npy4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)\npy4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)\npy4j.GatewayConnection.run(GatewayConnection.java:214)\njava.lang.Thread.run(Thread.java:748),
schedulingPool:“默认值”,
累加器更新:
[[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象],
[对象]],
任务:{0':[Object]},
executorSummary:{0':[Object]}]
我现在需要提取launchTime和taskTime。这是如何做到的?我以前曾设法从JSON中提取数据,但我认为我在这里遇到了问题,因为它包含在一个数组中。您可以使用它来迭代响应体的每个成员
res.on('end', () => {
let body = JSON.parse(Buffer.concat(chunks))
body.forEach(item => {
// Do something with item
console.log(item)
})
})
您可以使用来迭代响应体的每个成员
res.on('end', () => {
let body = JSON.parse(Buffer.concat(chunks))
body.forEach(item => {
// Do something with item
console.log(item)
})
})
如果数组长度始终为1,则可以使用
body[0]
访问第一个数组元素
body[0].executorRunTime
body[0].firstTaskLaunchedTime
否则,可以使用
body.forEach()
迭代结果。如果数组长度始终为1,则可以使用body[0]
访问第一个数组元素
body[0].executorRunTime
body[0].firstTaskLaunchedTime
否则,您可以使用
body.forEach()
对结果进行迭代。数组长度是否始终为1?Jim的anwer将始终为1。感谢您的回答。数组长度是否始终为1?Jim的anwer将始终为1。感谢您的回答。