我使用 Nodejs 和 mongoskin 将数据以 JSON 格式存储在 mongo 表中。 Mongo 自动在 json 格式的数据中添加斜线。然后当我检索时,我无法解析数据。请建议我,如何删除斜杠或如何解析数据?
db.messagetable.find().forEach(printjson)
{
"_id" : "1861574",
"MatriId" : "1861574",
"session" : "{\"messages\":{\"cometchat\":{\"timedifference\":0,\"cometchat_buddytime\":0,\"msgavails\":\"\"}}}",
"expires" : 1341702134
}
最佳答案
这里的斜杠用于转义 JSON 编码字符串中的字符 "
(不是 json 对象,因为
> JSON.parse("{messages:1}")
SyntaxError: Unexpected token m
at Object.parse (native)
at repl:1:7
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
因此键必须用 "
括起来以表示一个字符串,但您不能只将 "
放在字符串中。要修复 json 解析器,请附加 \
> JSON.parse("{\"messages\":1}")
{ messages: 1 }
所以当你解析时,只需在 session 字符串上调用 JSON.Parse
> JSON.parse("{\"messages\":{\"cometchat\":{\"timedifference\":0,\"cometchat_buddytime\":0,\"msgavails\":\"\"}}}")
{ messages:
{ cometchat:
{ timedifference: 0,
cometchat_buddytime: 0,
msgavails: '' } } }
关于node.js - 如何使用 Nodejs 删除 Mongodb 中 JSON 格式数据中的斜线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8968248/