我有一个由Apache Flink Tool编写的文本文件。 1.我想提取这个文件的内容,直到它遇到第999个"}," 2.删除最后一个逗号(,) 3.添加" ["和"]"在阅读内容的开头和结尾。 4.将此修改后的内容写入变量。
所有这些都要写入变量。
通过Javascript可以实现整个操作吗?
此操作的目的是使其类似于用于绘制Highcharts的JSON数组,一次只能加载1000个点。
fxt文件的内容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"
}
]
答案 0 :(得分:0)
如何,这将返回一个如下所示的数组:
[
[
{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)
&#13;