简体   繁体   English

我有一个返回为json的cookie,但无法获取该值?

[英]I have a cookie that comes back as json but can't get the value?

I have this cookie 我有这个饼干

 {"__ngDebug":"true","userdata":"j:{\"id\":10099,\"username\":\"somename@gmail.com\",\"email\":\"somename@gmail.com\",\"role\":\"candidate\"}"}

If I do cookie.userdata I get 如果我做cookie.userdata我得到

"j:{\"id\":10099,\"username\":\"somename@gmail.com\",\"email\":\"somename@gmail.com\",\"role\":\"candidate\"}"

If I do cookie.userdata.j I get nothing. 如果我执行cookie.userdata.j我什么也cookie.userdata.j得到。 What do I have to do to get email, username, and role? 我该怎么办才能获取电子邮件,用户名和角色?

The Problem 问题

This is because the result of cookie.userdata is a string not an object . 这是因为cookie.userdata的结果是string而不是object

Unfortunately you can not directly parse the result as JSON with JSON.parse , because the value of userdata is not valid JSON (It is object syntax without being wrapped in { and } , and j needs to be wrapped in " ) 不幸的是,您不能直接使用JSON.parse将结果解析为JSON,因为userdata的值不是有效的JSON(这是对象语法,没有被包装在{} ,而j需要被包装在"

Best Method: Fix Input At Source 最佳方法:在源代码处修复输入

To solve this issue you need to reformat you input string if possible like so, 要解决此问题,您需要尽可能重新设置输入字符串的格式,

{"__ngDebug":"true","userdata":{"j":{"id":10099,"username":"somename@gmail.com","email":"somename@gmail.com","role":"candidate"}}}

Note I added { and } to the beginning and end of userdata and I wrapped j in " . Also I made removed the \\ and the " around the value of userdata since we want it to be an object not a string . 注意我添加{}的开始和结束userdata和我包j"我也做了删除\\"周围的人的价值userdata ,因为我们希望它是一个object不是一个string

Doing this, you should be able to get the value of j like so cookie.userdata.j . 这样做,您应该能够像cookie.userdata.j这样获得j的值。

If you for some reason must have j as a string , this will also work, 如果您出于某种原因必须j作为string ,这也可以使用,

{"__ngDebug":"true","userdata":"{\"j\":{\"id\":10099,\"username\":\"somename@gmail.com\",\"email\":\"somename@gmail.com\",\"role\":\"candidate\"}}"}

Note I added { and } to the beginning and end of userdata and I wrapped j in " 注意我添加{}的开始和结束userdata和我包j"

Then you should be able to get it with JSON.parse(cookie.userdata).j 然后您应该可以使用JSON.parse(cookie.userdata).j来获取它

Not As Good Method: But it works 不是很好的方法:但是有效

If you can't fix the input at the source for some reason, you can do some fiddling with the cookie.userdata string to make it valid JSON, then use JSON.parse on it. 如果由于某种原因无法在源代码处固定输入,则可以对cookie.userdata string进行一些摆弄,以使其成为有效的JSON,然后在其上使用JSON.parse

Assuming cookie.userdata yields the following, 假设cookie.userdata产生以下内容,

"j:{\"id\":10099,\"username\":\"somename@gmail.com\",\"email\":\"somename@gmail.com\",\"role\":\"candidate\"}"

Then you can do the following to coerce it into a valid object. 然后,您可以执行以下操作将其强制为有效对象。

val = cookie.userdata
objVal = JSON.parse('{' + val.replace('j:', '"j":') + '}')
j = objVal.j

What I am doing here is adding the JSON object delimiters { and } and wrapping j in " , which is required by the JSON standard. 我在这里所做的是添加JSON对象定界符{}并将j包装在" ,这是JSON标准所必需的。

Conclusion 结论

Using any of the above methods, once you have j as in object, it is a simple matter to get the username , email , and role . 使用上述任何一种方法,只要在对象中具有j作为对象,就可以轻松获取usernameemailrole

j.username => "somename@gmail.com"
j.email => "somename@gmail.com"
j.role => "candidate"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM