【发布时间】:2016-12-30 11:18:25
【问题描述】:
我在从文本文件创建结构化 JSON 时遇到了一些困难。这是一个文本文件的例子
BlocA
value 1 param X
value 2 param Y
value 3 param Z
BlocB
this is a line
this is another line
BlocC foo
1
2
3
BlocC bar
something else
我想要的 JSON 是这样的:
{
"BlocA" : [
"value 1 param X",
"value 2 param Y",
"value 3 param Z"
]
},
"BlocB" : [
"this is a line",
"this is another line"
]
},
"BlocC" : [
{"foo" : [1,2,3]},
{"bar" : ["something else"]}
]
}
在我的文本文件中,块总是从一行的第一个字符开始,块的属性如下所示,并且总是至少以一个空格字符开始。
有时,块名称后跟一个字符串(如“BlocC foo”和“BlocC bar”),它成为 BlocC 数组的子文档。
我正在使用 nodeJS 来做到这一点。我虽然使用递归来做到这一点,但这对我来说有点太棘手了。
还有其他方法可以实现这一点(使用解析器或其他)?
感谢您的帮助。
【问题讨论】:
标签: json node.js parsing recursion