[英]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? 我该怎么办才能获取电子邮件,用户名和角色?
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
需要被包装在"
)
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
来获取它
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标准所必需的。
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
作为对象,就可以轻松获取username
, email
和role
。
j.username => "somename@gmail.com"
j.email => "somename@gmail.com"
j.role => "candidate"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.