[英]Using the data from an HTTP request in node.js
我的任務是從http://services.swpc.noaa.gov/text/ace-swepam.txt獲取數據並將其拆分/排序為有用的東西。 首先,我試圖將數據拆分為類別,以便我可以在chart.js中使用或稍后使用,但是當我嘗試打印字段時,它只是以[]的形式出現。
var options = {
host: 'services.swpc.noaa.gov',
path: '/text/ace-swepam.txt',
port: 80,
method: 'POST'
};
var req = http.request(options, function (res) {
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
//delete header lines
lines.splice(0, 18);
if (lines.length <= 20) {
return;
}
console.log(lines);
});
res.on('end', function (e) {
callback();
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
function callback() {
for (var line in lines) {
var x = [];
x = lines[line].split(" ");
var statuscode = x[14];
if (statuscode == 0) {
if (lines[line].indexOf('-') === -1) {
year.push(x[0]);
month.push(x[1]);
day.push(x[2]);
time.push(x[4]);
statusno.push(statuscode);
proton.push(x[22]);
bulksp.push(x[28]);
iontemp.push(x[33]);
}
}
}
// console.log(year, month, day, time, statusno, proton, bulksp, iontemp)
}
數據行似乎不是制表符分隔的。 我希望它們是固定長度的。
這是我收到的第一行。 “2015 08 18 1708 57252 61680 0 2.6 45”
而不是嘗試按標簽拆分此行。
fields = line.split("\t");
創建每個字段長度的數組,並使用substring方法將其拆分。
這是解析返回數據的完整代碼。 它給出119行或6-7有一個狀態!= 0(因此被跳過)。 然后,您的變量各有112個條目。
res.on('data', function (chunk) {
var fieldLengths = [0, 4, 7, 10, 16, 24, 32, 37, 48, 59, 72];
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
// for me, the first "chunk" is incomplete. Throw it away and just use the second chunk.
if (lines.length <= 20) {
return;
}
//delete header lines
lines.splice(0, 18);
for (var line in lines) {
console.log("entry: " + lines[line]);
//split into data fields
var lineText = lines[line];
var fields = [];
for (var i = 0; i <= fieldLengths.length -1; i++) {
fields.push(lineText.substring(fieldLengths[i], fieldLengths[i + 1]));
}
//if there are no problems (status code 0)
//add the data to their respective fields
if (fields[6] == 0) {
year.push(fields[0]);
month.push(fields[1]);
day.push(fields[2]);
time.push(fields[3]);
statusno.push(fields[6]);
proton.push(fields[7]);
bulksp.push(fields[8]);
iontemp.push(fields[9]);
}
}
});
res.on('end', function (e) {
console.log(year);
});
});
如果您使用Visual Studio(免費社區版將起作用)並為visual studio添加節點工具,這很容易調試。
如果數據不太合適,請告訴我。 我理解你要做什么,並且可以在必要時調整代碼。
function callback() {
for (var line in lines) {
//split into data fields
year.push(lines[line].substring(x,y));//fill in x and y
//month.push..
//day.push..
}
}
要么
callback() {
var x = [];
for (var line in lines){
x = lines[line].split(" ");
console.log(x);
year.push(x[index]) // index being where year was split into x
}
}
把這個函數放在res.on('end')中。 我不是百分百肯定你在做什么,希望這會有所幫助。
編輯:
var options = {
host: 'services.swpc.noaa.gov',
path: '/text/ace-swepam.txt',
port: 80,
method: 'POST'
};
var req = http.request(options, function (res) {
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
results += chunk.toString();
//split results into an array by each new line
lines = results.split("\n");
//delete header lines
lines.splice(0, 18);
});
res.on('end', function (e) {
callback();
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
function callback() {
for (var line in lines) {
var x = [];
x = lines[line].split(" ");
//console.log(x); Print x and see which index of x has the vaule you want. Constant for all
year.push(x[0]);
month.push(x[1]);
day.push(x[2]);
time.push(x[4]);
}
//console.log(year,month,day,time); Check final result
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.