我正在使用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
答案 0 :(得分:0)
你为什么不这样做?它应该适用于任何返回的结构(注意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);