0--*_*0-- 3 streaming logging parsing json node.js
使用 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
1) 这是有效的 JSON 吗?2)为什么双引号会变成单引号?
读入后,解析出 JSON,并显示它,我得到以下信息:
{
'petstore': 'store_number_8',
'dogs':{
'terrier':{
'total':2
}
},
'cat':{
'siamese':{
'total':5
}
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我正在使用 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);
}
Run Code Online (Sandbox Code Playgroud)
先感谢您。
console.log不返回 JSON。它返回此对象的人类可读表示。
所以不,它不是 JSON,它更接近 JSON5。
如果要显示JSON,则必须将字符串传递给console.log,即 console.log(JSON.stringify(shouldThisBeValidJSON))
| 归档时间: |
|
| 查看次数: |
1768 次 |
| 最近记录: |