Javascript 当读取Json对象数组并创建csv文件时,第一列是从第二条记录中提取的
我有这个JSON对象数组(摘录)。在我的Javascript中,我使用迭代函数创建了一个csv,其中包含我想要的值。问题是csv的第一条记录创建正确,但从第二条记录开始,向前创建的第一列为空。它的第一个字符是逗号,我不知道是什么在我的逻辑中导致了它。你能帮忙吗?请参阅下面的代码:Javascript 当读取Json对象数组并创建csv文件时,第一列是从第二条记录中提取的,javascript,arrays,json,node.js,csv,Javascript,Arrays,Json,Node.js,Csv,我有这个JSON对象数组(摘录)。在我的Javascript中,我使用迭代函数创建了一个csv,其中包含我想要的值。问题是csv的第一条记录创建正确,但从第二条记录开始,向前创建的第一列为空。它的第一个字符是逗号,我不知道是什么在我的逻辑中导致了它。你能帮忙吗?请参阅下面的代码: "recs-count": 139, "gps-recs": [ { "RecId": 40020551513, "City": "New Port Richey",
"recs-count": 139,
"gps-recs": [
{
"RecId": 40020551513,
"City": "New Port Richey",
"Country": "USA",
"County": "PASCO",
"State": "FL",
"Street": "1546 Amaryllis ct.",
"ZipCode": "34655",
"CurrentOdometer": 12161,
"Heading": 0,
"Latitude": 28.181690,
"Longitude": -82.658420,
"SpeedMph": 0,
"SpeedLimitMph": 25,
"Status": "Stopped",
"StatusDuration": "1.05:00:00",
"PrimaryLandmarkName": "Max's home",
"CurrentHardmountEvent": null,
"UtcTimeTag": "2013-11-28T05:23:34",
"UserTimeTag": "2013-11-28T00:23:34",
"UserInfo": {
"UserId": 201274,
"UserNumber": "22",
"UserName": "Max's Car"
}
},
{
"RecId": 40020551610,
"City": "New Port Richey",
"Country": "USA",
"County": "PASCO",
"State": "FL",
"Street": "1546 Amaryllis ct.",
"ZipCode": "34655",
"CurrentOdometer": 12161,
"Heading": 0,
"Latitude": 28.181690,
"Longitude": -82.658410,
代码:
request(options, function (error, response, body) {
if (error) throw new Error(error);
var result = JSON.parse(body)['gps-recs'];
console.log(result);
console.log(Object.keys(result).length);
//console.log(body);
buildCSV(result);
});
function buildCSV(result) {
// loop runs result.length times
for (i = 0; i < Object.keys(result).length; i++) {
csvFile.push(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n' );
console.log(csvFile[i]);
};
fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
这是解析后JSON对象的外观:
工作代码如下:
我必须将带有CSV格式数据的数组更改为字符串(变量)。fs.write模块需要字符串,而不是数组。这就是为什么数据在经过fs写入模块后被更改的原因。谢谢大家的帮助
var request = require("request");
var fs = require ('fs');
var csvFile = "";
var options = { method: 'GET',
url: 'URL',
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
var result = JSON.parse(body)['gps-recs'];
console.log(Object.keys(result).length);
console.log(result.length);
buildCSV(result);
});
function buildCSV(result) {
// loop runs result.length times
for (var i = 0; i < result.length; i++) {
csvFile = csvFile.concat(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n');
};
console.log(csvFile);
fs.writeFile('file.csv', csvFile, function(err) {
//fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
var请求=要求(“请求”);
var fs=需要('fs');
var csvFile=“”;
var options={method:'GET',
url:'url',
};
请求(选项、功能(错误、响应、正文){
如果(错误)抛出新错误(错误);
var result=JSON.parse(body)['gps-recs'];
console.log(Object.keys(result.length));
console.log(结果.长度);
构建CSV(结果);
});
函数buildCSV(结果){
//循环运行result.length次
对于(变量i=0;i
问题似乎在于buildCSV方法正在迭代对象上定义的成员,而不是集合中的记录数
您希望将您的方法定义为:
function buildCSV(result) {
// loop runs result.length times
for (i = 0; i < result.length; i++) { // **this was changed**
csvFile.push(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n' );
console.log(csvFile[i]);
};
fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
函数buildCSV(结果){
//循环运行result.length次
对于(i=0;i
您正在使用的属性本质上是枚举对象上定义的所有属性,而您似乎希望在数组中循环。问题似乎在于buildCSV方法正在迭代对象上定义的成员,而不是集合中的记录数 您希望将您的方法定义为:
function buildCSV(result) {
// loop runs result.length times
for (i = 0; i < result.length; i++) { // **this was changed**
csvFile.push(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n' );
console.log(csvFile[i]);
};
fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
函数buildCSV(结果){
//循环运行result.length次
对于(i=0;i
您正在使用的属性本质上是枚举对象上定义的所有属性,而您似乎希望在数组中循环。问题似乎在于buildCSV方法正在迭代对象上定义的成员,而不是集合中的记录数 您希望将您的方法定义为:
function buildCSV(result) {
// loop runs result.length times
for (i = 0; i < result.length; i++) { // **this was changed**
csvFile.push(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n' );
console.log(csvFile[i]);
};
fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
函数buildCSV(结果){
//循环运行result.length次
对于(i=0;i
您正在使用的属性本质上是枚举对象上定义的所有属性,而您似乎希望在数组中循环。问题似乎在于buildCSV方法正在迭代对象上定义的成员,而不是集合中的记录数 您希望将您的方法定义为:
function buildCSV(result) {
// loop runs result.length times
for (i = 0; i < result.length; i++) { // **this was changed**
csvFile.push(result[i].UserInfo.UserNumber + ',' + result[i].UtcTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].Speed + ',' + result[i].Heading + '\r\n' );
console.log(csvFile[i]);
};
fs.writeFile('file.csv', csvFile, function(err) {
if (err) throw err;
console.log('file saved');
});
};
函数buildCSV(结果){
//循环运行result.length次
对于(i=0;i
您正在使用的属性本质上是枚举对象上定义的所有属性,而您似乎希望在数组中循环。看起来您正在构建一个名为cvsFile的数组,其中数组中的每个元素表示文件中的一行。然后将数组传递给writeFile方法。writeFile方法需要字符串或缓冲区。writeFile方法通过使用“,”作为分隔符连接元素来将数组转换为字符串。我相信您需要通过使用空字符串连接元素来自己创建字符串作为分隔符。然后可以将该字符串传递给writeFile方法。例如
fs.writeFile('file.csv', csvFile.join(""), function(err) {
if (err) throw err;
console.log('file saved');
});
看起来你是b