如何将复杂的json保存为redis中的字符串并将其作为未转义的合法json对象检索

时间:2015-06-10 12:28:55

标签: jquery node.js redis

最终目标是在redis实例中保存并检索json为字符串,我从外部api获取此json并希望将其保存在我的redis实例中。

我能够使用节点使用redis模块保存文档。现在,当我尝试检索它时,它给了我一个转义的json字符串。以下是我将数据保存到redis的代码:

app.get('/util/util1/:key', function(req, res) {
  res.type('application/json');
  var name = req.params.key;
 res.header("Access-Control-Allow-Origin", "*");

  var responseserv;
  client.get(name, function(err, reply) {

    console.log("Parameter: " + JSON.stringify(JSON.parse(reply)));
    //var unescapedstring = JSON.stringify(JSON.parse(reply),null,4);
    //console.log(unescapedstring);
    res.send(deepUnescapeJSON(JSON.parse(reply)));   
    });
});

获取密钥值的代码如下:

app.post('/util/util1/addtoredis/:key', function(req, res) {
    res.type('application/json');

    var redvalue = JSON.stringify(req.body);
    console.log("Body : " + req.url+" body : "+redvalue);
    var redkey = req.params.key;
    console.log("key & value    :: " + redkey + " , " + (redvalue));
    client.set(redkey, redvalue);
    //console.log("IP Details : "+myIP());
    //console.log("Got request: " + JSON.stringify(redvalue));
    res.send(redvalue);     
});

我发现如果我将json存储在redis中,它将被存储为转义字符串,而当我获取它时,它将被检索为相同的转义字符串。

以下是json示例:

{
    "name": "Sample newspaper",
    "description": "Sample newspaper",
    "expiredOn": "22-nov-2010",
    "isDynamic": false,
    "isEnabled": false,
    "startedOn": "22-oct-2010",
    "updatedOn": "",
    "updatedBy": "admin",
    "newspaperRules": [
        {
            "type": "4",
            "buyCount": "0",
            "buyAmount": "200",
            "placeHolderText": "Helix times",
            "count": 1,
            "id": "",
            "newspaperFreeItems": [
                {
                    "id": "",
                    "groupId": 1,
                    "itemId": "23456",
                    "type": "1",
                    "isActive": 1
                }
            ]
        }
    ]
}

0 个答案:

没有答案