将文本从文件转换为javascript对象
我试图读取一个文本文件,并使用NodeJS将其转换为javascript对象。我的文件如下所示:将文本从文件转换为javascript对象,javascript,node.js,readfile,Javascript,Node.js,Readfile,我试图读取一个文本文件,并使用NodeJS将其转换为javascript对象。我的文件如下所示: Package: libseccomp2 Depends: libc6 (>= 2.14) Description: high level interface to Linux seccomp filter This library provides a high level interface to constructing, analyzing and installing secco
Package: libseccomp2
Depends: libc6 (>= 2.14)
Description: high level interface to Linux seccomp filter
This library provides a high level interface to constructing, analyzing
and installing seccomp filters via a BPF passed to the Linux Kernel's
prctl() syscall.
Package: libperl5.26
Depends: libbz2-1.0, libc6 (>= 2.23), libdb5.3, libgdbm-compat4, libgdbm5 (>= 1.12), zlib1g (>= 1:1.2.2.3), perl-modules-5.26 (>= 5.26.1-6ubuntu0.3)
Description: shared Perl library
This package contains the shared Perl library, used by applications
which embed a Perl interpreter.
.
It also contains the architecture-dependent parts of the standard
library (and depends on perl-modules-5.26 which contains the
architecture-independent parts).
我想这样输出它:
[{
Package: "libseccomp2",
Depends: ["libc6"]
Description: "high level interface to Linux seccomp filter
This library provides a high level interface to constructing, analyzing
and installing seccomp filters via a BPF passed to the Linux Kernel's
prctl() syscall."
},
{
Package: "libperl5.26",
Depends: ["libbz2-1.0", "libc6"]
Description: "high level interface to Linux seccomp filter
This library provides a high level interface to constructing, analyzing
and installing seccomp filters via a BPF passed to the Linux Kernel's
prctl() syscall."
}]
这是我到目前为止所拥有的,但它不断给我错误。提前感谢您的帮助
fs.readFile('file.txt', "utf8", (err, data) => {
if (err) {
console.error(err)
return
}
let obj = {};
let line = data.toString().split("\n");
for (let i = 0; i<line.length; i++) {
let newLine = line[i].split(":");
obj[newLine[0]] = newLine[1].split(" ");
}
console.log(obj);
})
fs.readFile('file.txt',utf8',(错误,数据)=>{
如果(错误){
控制台错误(err)
返回
}
设obj={};
让line=data.toString().split(“\n”);
对于(让i=0;i您的源文件看起来像是从YAML文件派生的。如果格式正确,您可以使用npm库(如“YAML”)将文件直接解析为JS对象
格式正确的YAML文件如下所示:
-
Package: libseccomp2
Depends: libc6 (>= 2.14)
Description: high level interface to Linux seccomp filter
This library provides a high level interface to constructing, analyzing and installing seccomp filters via a BPF passed to the Linux Kernel's prctl() syscall.
-
Package: libperl5.26
Depends: libbz2-1.0, libc6 (>= 2.23), libdb5.3, libgdbm-compat4, libgdbm5 (>= 1.12), zlib1g (>= 1:1.2.2.3), perl-modules-5.26 (>= 5.26.1-6ubuntu0.3)
Description: shared Perl library
This package contains the shared Perl library, used by applications
which embed a Perl interpreter.
.
It also contains the architecture-dependent parts of the standard
library (and depends on perl-modules-5.26 which contains the
architecture-independent parts).
我会检查是否可以首先将源文本文件生成为正确的YAML,或者如果您仍然使用当前文本文件,请尝试使用。您遇到了哪些错误?您是否尝试调试这些错误?从我看到的,换行[1]。拆分(“”)
不包含“:”的行可能有问题,因为换行符[1]
将被取消定义该文件看起来确实像YAML,但由于该文件包含大量的/
,验证程序说它不是有效的,因为它当前的格式无效。我在上面发布了一个更正的版本。请尝试通过YAML到JSON转换器运行我的版本,您将获得所需的格式。y和我们和我的版本是缩进,项目分隔符“-”。