如何通过提供的属性名称列表从文本形式中解析基于键值的数据并将其写回JSON-Format?
我正在尝试编写一个包含类似文本的匹配单词的JSON文件,
servicepoint 200135644 watchid 7038842
因此每次使用此代码仅一次将每个ServicePoint和WatchID插入到对象表中:
function readfile() {
Tesseract.recognize('form.png', 'ara', {
logger: m => console.log(m)
}).then(({ data: { text } }) => {
console.log(text); /* this line here */
var obj = {
table: []
};
const info = ['servicepoint', 'watchid'];
for (k = 0; k < info.length; k++) {
var result = text.match(new RegExp(info[k] + '\\s+(\\w+)'))[1];
obj.table.push({
servicepoint: /* Here i want to insert the number after servicepoint*/ ,
watchid: /*also i want to insert the number after watchid to the Object table*/
});
}
var json = JSON.stringify(obj); /* converting the object table to json file*/
var fs = require('fs'); /* and then write json file contians the data*/
fs.writeFile('myjsonfile.json', json, 'utf8', callback);
})
};
I'm trying to write a JSON file that contains a matched words from a text like this
servicepoint 200135644 watchid 7038842
so each servicepoint and watchid will be inserted to the Object table only one time using this code:
function readfile() {
Tesseract.recognize('form.png', 'ara', {
logger: m => console.log(m)
}).then(({ data: { text } }) => {
console.log(text); /* this line here */
var obj = {
table: []
};
const info = ['servicepoint', 'watchid'];
for (k = 0; k < info.length; k++) {
var result = text.match(new RegExp(info[k] + '\\s+(\\w+)'))[1];
obj.table.push({
servicepoint: /* Here i want to insert the number after servicepoint*/ ,
watchid: /*also i want to insert the number after watchid to the Object table*/
});
}
var json = JSON.stringify(obj); /* converting the object table to json file*/
var fs = require('fs'); /* and then write json file contians the data*/
fs.writeFile('myjsonfile.json', json, 'utf8', callback);
})
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从我上述评论之一...
除了提出的模式更改外,我还将更改OP的 thenables 进入更明确的任务,例如将文件阅读与分开,只是数据解析/提取与 json-conversion/Writing 。
我还想指出,OP的所需
data.table
基于格式不可能是数组,而必须是纯键值结构(对象),因为一个人只能汇总一个对象(一个对象(一个一个对象)迭代属性名称时,或者仅将单个属性项目(在迭代属性名称迭代时,一个项目)时,请按时进入数组。 (尽管也尝试创建一个多输入对象,但也将其推动。)下一个提供的代码显示了该方法。实现遵循OP的原始代码。它只是使用异步 - 瓦特语法并模拟/伪造文件阅读和写作过程。
From one of my above comments ...
In addition to the proposed pattern change I also would change the OP's thenables into more clear tasks like separating file-reading from just data parsing/extraction from json-conversion/writing.
I also want to state that the OP's desired
data.table
based format can not be an array but has to be a pure key-value structure (object) since one can just either aggregate a single object (one entry at time while iterating the property names) or push single property only items (one item at time while iterating the property names) into an array. (The OP tries to create a multi-entry object though also pushing it.)The next provided code shows the approach. The implementation follows the OP's original code. It just uses async-await syntax and mocks/fakes the file reading and writing processes.
您可以使用
String.Match
获取两个键所需的变量值:“ ServicePoint”和“ WatchID”。我建议使用此匹配模式以获取您的两个数据点。
然后,您需要创建然后将JSON串起,为您提供类似的东西:
{servicepoint:1323,watchID:234}
我假设您有很多这样的行,因此您需要添加每个json键值到一个数组。然后,您可以
json.stringify(dataarray)
生成有效的JSON文本以写入文件。You can use
String.match
to get the required variable values for your two keys: "servicepoint" and "watchid".I would suggest using this match pattern to get your two data points.
Then you'll need to create and then stringify the JSON, giving you something like:
{servicepoint: 1323, watchid: 234}
I assume you have many of these rows, so you'll want to add each JSON key-value to an array. You can then
JSON.stringify(dataArray)
to generate the valid JSON text to write to a file.