Redis将“\”变为JSON字符串

时间:2017-04-14 09:21:20

标签: json redis

我正在使用Redis保存JSON的值并稍后检索它。问题是当我从redis中检索值时,它会添加多个" \"这毁了我的JSON。

{ "user": "123456", "password": "xxxxxxx" }

client.lrange('message', 0, -1, function (error, items) {
 if (error) throw error
  const response = {
    statusCode: 200,
    body: JSON.stringify({
    message: items
  }),
 };
 callback(null, response);
})

当我检索到它的价值时出现:

{ \"user\": \"123456\", \"password\": \"xxxxxxx\" }

1 个答案:

答案 0 :(得分:0)

您的Redis JSON.stringify返回的回复正在运行LRANGE。虽然你喜欢这个命令来返回一个JSON对象列表 - 对于Redis来说,它是一个字符串列表,而且是你的代码所得到的。 stringify导致双重推文,因为你从未在数组内部的字符串顶部运行JSON.parse