gpt4 book ai didi

json - 读取日志时,JSON 从双引号变为单引号

转载 作者:行者123 更新时间:2023-12-01 12:43:57 27 4
gpt4 key购买 nike

通过 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);
}

提前致谢。

最佳答案

console.log 不返回 JSON。它返回此对象的人类可读表示。

所以不,它不是 JSON,它更接近 JSON5。

如果要显示 JSON,则必须将字符串传递给 console.log,即 console.log(JSON.stringify(shouldThisBeValidJSON))

关于json - 读取日志时,JSON 从双引号变为单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com