gpt4 book ai didi

javascript - 读取 Json 对象数组并创建 csv 文件时,第一列是从第二条记录开始的

转载 作者:行者123 更新时间:2023-11-28 18:56:59 25 4
gpt4 key购买 nike

我有这个 JSON 对象数组(摘录)。在我的 Javascript 中,我正在使用迭代函数创建一个包含我想要的值的 csv。问题是 csv 的第一条记录已正确创建,但从第二条记录开始创建一个空的第一列。它有一个逗号作为第一个字符,我不知道我的逻辑是什么导致了它。你能帮我吗?请参阅下面的代码:

"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');
});
};

这是创建的文件(已查看 txt):

2169266287,2015-10-27T04:00:05,25.796945,-80.255766,undefined,100,
,2164309328,2015-10-27T04:00:20,42.224414,-83.347603,undefined,0,
,2162029911,2015-10-27T04:00:46,41.500245,-81.681383,undefined,0,
,2164328304,2015-10-27T04:01:19,42.238847,-83.32803,undefined,0,
,2164309328,2015-10-27T04:01:28,42.22442,-83.347484,undefined,0,
,2162029911,2015-10-27T04:01:46,41.500245,-81.681383,undefined,0,
,2164328304,2015-10-27T04:02:23,42.23782,-83.328911,undefined,0,
,2164309328,2015-10-27T04:02:39,42.224411,-83.34745,undefined,0,
,2162029911,2015-10-27T04:02:46,41.50021,-81.681347,undefined,140,
,2164328304,2015-10-27T04:03:12,42.238956,-83.325409,undefined,0,
,2169655749,2015-10-27T04:03:24,25.797042,-80.260972,undefined,136,
,2162029911,2015-10-27T04:03:46,41.50021,-81.681347,undefined,0,
,2164309328,2015-10-27T04:03:49,42.224419,-83.347445,undefined,0,
,2162029911,2015-10-27T04:04:46,41.50021,-81.681347,undefined,0,
,2164309328,2015-10-27T04:05:05,42.22442,-83.347456,undefined,0,
,2164328304,2015-10-27T04:05:29,42.238716,-83.327105,undefined,211,
,2162029911,2015-10-27T04:05:46,41.50021,-81.681347,undefined,0,
,2164309328,2015-10-27T04:06:28,42.22441,-83.347477,undefined,0,
,2164328304,2015-10-27T04:06:32,42.239021,-83.32724,undefined,256,

这是解析后 JSON 对象的样子: enter image description here

工作代码如下:我必须将包含 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');
});
};

最佳答案

看起来您正在构建一个名为 cvsFile 的数组,其中数组中的每个元素代表文件中的一行。然后将数组传递给 writeFile 方法。 writeFile 方法需要一个字符串或缓冲区。 writeFile 方法通过使用“,”作为分隔符连接元素,将数组转换为字符串。我相信您需要通过使用空字符串“”作为分隔符连接元素来自己创建字符串。然后您可以将该字符串传递给 writeFile 方法。例如...

fs.writeFile('file.csv', csvFile.join(""), function(err) {
if (err) throw err;
console.log('file saved');
});

关于javascript - 读取 Json 对象数组并创建 csv 文件时,第一列是从第二条记录开始的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com