【发布时间】:2014-03-18 02:39:33
【问题描述】:
通过 apache 日志(如下),我能够解析出 JSON:
[2014.02.14_21.24.22.543] other info I don't care about json: {
"petstore": "store_number_8",
"dogs":{
"terrier":{
"total":2
}
},
"cat":{
"siamese":{
"total":5
}
}
}
1) 这是有效的 JSON 吗? 2)为什么双引号会变成单引号?
读入后,解析出 JSON,并显示它,我得到以下信息:
{
'petstore': 'store_number_8',
'dogs':{
'terrier':{
'total':2
}
},
'cat':{
'siamese':{
'total':5
}
}
}
顺便说一句,我正在使用 Node.js 的 fs.createStream 来读取日志,然后简单地输出控制台(到目前为止,我没有进行任何清理,最终我会将其写入文件)。
fs.creatReadStream(logs).pipe(split()).on(data, function(line){
if(line.match(/json\:/)){
shouldThisBeValidJSON = JSON.parse(line.slice(line.indexOf('{'), line.length));
console.log(shouldThisBeValidJSON);
}
提前谢谢你。
【问题讨论】:
标签: json node.js parsing logging streaming