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

'How to read a file content until 'n' occurrences of a character and write it into a variable", using javascript?

我有一个由 Apache Flink Tool 编写的文本文件。 1.我想提取这个文件的内容,直到它遇到第999个“}”, 2.删除最后一个逗号(,) 3、在阅读内容的开头和结尾添加“[”和“]”。 4.把这个修改后的内容写到一个变量中。

这些都要写入一个变量中

是否可以通过Javascript完成整个操作?

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

Flink 正在写入的 txt 文件内容:

{"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"
    }
]

怎么样,这将 return 一个如下所示的数组:

[
    [
        {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)