我正在使用Sencha Connect cookie会话。它在Node中工作得很好,但是我需要在客户端解析cookie。 Connect将s:j:添加到cookie的前面,并在末尾使用点符号+生成的键。有没有办法防止这种情况或在JS中对此进行解析,还是我需要编写自己的解析器?
Cookie值如下所示:

s: j: {
    "oauth": {
        "token": "xxxxxx",
        "token_secret": "xxxxxx",
        "results": {
            "oauth_callback_confirmed": "true"
        },
        "access_token": "xxxxx-xxxx",
        "access_token_secret": "xxxx"
    },
    "me": {
        "val": "bu-rcizj7u8jm7jzgea",
        "refs": {
            "bu-rcizj7u8jm7jzgea": {
                "id": "bu-rcizj7u8jm7jzgea",
                "creation_time": "2012-07-13T16:01:17.159+0000",
                "twitter_id": "138031018",
                "first_name": "xxx",
                "last_name": "xxx",
                "email": "xxx@xxx.com",
                "permission": {
                    "can_update": false,
                    "can_delete": false,
                    "can_read": false
                },
                "twitter_screen_name": "xxxx"
            }
        }
    }
}.DdFx96zckmMbWY8wLiFhuGwnofluk3x2WSVBv71LOV8

最佳答案

你为什么不做这样的事情?它应该适用于任何返回的结构(如果未找到,请注意indexOf返回-1,并且'}。'通常不是有效的JSON,因此在实际有效的JSON情况下,它只会读到末尾)。一种替代方法是编写Dijkstra算法的实现,该算法仅在找到开头'{'时才开始推入值堆栈。这会稍微快一点,因为它只会是一次通过(而两次调用indexOf会使我的实现在最糟糕的情况下是三遍)。如果您有兴趣,我很乐意发送一次验证码。

var rawCookie = 's: j: { "oauth": { "token": "xxxxxx", "token_secret": "xxxxxx", "results": { "oauth_callback_confirmed": "true" },"access_token": "xxxxx-xxxx", "access_token_secret": "xxxx"}, "me": {"val": "bu-rcizj7u8jm7jzgea","refs": {"bu-rcizj7u8jm7jzgea": { "id": "bu-rcizj7u8jm7jzgea", "creation_time": "2012-07-13T16:01:17.159+0000", "twitter_id": "138031018", "first_name": "xxx", "last_name": "xxx", "email": "xxx@xxx.com", "permission": { "can_update": false, "can_delete": false,"can_read": false }, "twitter_screen_name": "xxxx" } } } }.DdFx96zckmMbWY8wLiFhuGwnofluk3x2WSVBv71LOV8';
var niceJsonCookie = rawCookie.slice(rawCookie.indexOf('{'), rawCookie.indexOf('}.') + 1);
var niceJsonObject = JSON.parse(niceJsonCookie);

09-30 19:12
查看更多