"如何使用javascript读取文件内容,直到出现'n'个字符并将其写入变量"?



我有一个由Apache Flink工具编写的文本文件。1.我想提取此文件的内容,直到遇到999th"}",2.删除最后一个逗号(,(3.在读取内容的开始和结尾添加" [和"]。4.将此修改后的内容写入变量。

所有这些都将写入变量。

整个操作是否可以通过JavaScript?

此操作的目的是使其类似于一个用于绘制高音调的JSON数组,该数组只能一次加载1000点。

txt文件的内容flink写入:

{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454"},
{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523"},
{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524"},
{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525"},
{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"},
{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"},

执行上述所有列出的操作后要编写内容的(JSON(格式:

[
    {
        "temperSensorData": "28.489084691371996",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171424",
        "timestamp2": "1493270171454",
        "timestamp3": "1493270171454"
    },
    {
        "temperSensorData": "28.48908469137112",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171426",
        "timestamp2": "1493270171522",
        "timestamp3": "1493270171523"
    },
    {
        "temperSensorData": "28.489084691371186",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171426",
        "timestamp2": "1493270171523",
        "timestamp3": "1493270171524"
    }
]

如何返回看起来像这样的数组:

[
    [
        {999 ENTRIES}
    ],
    [
        {999 MORE ENTRIES}
    ]
    ...
]

var input = '{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454"}n{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523"}n{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524"}n{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525"}n{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"}n{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"}'
splitInput = input.split('n')
output = splitInput.map((item) => {
  return JSON.parse(item)
})
splitOutput = []
var chunk = 999;
for (var i=0,j=output.length; i<j; i+=chunk) {
    splitOutput.push(output.slice(i,i+chunk))
}
console.log(splitOutput)

相关内容

  • 没有找到相关文章

最新更新