In my Javascript app I make an API call to my Django server's /rest-auth/registration code
. If the registration attempt fails the API call returns the following JOSN which I am having problems parsing:
{
"_body":"{\"email\":[\"Enter a valid email address.\"],\"username\":[\"A user with that username already exists.\"]}",
"status":400,
"ok":false,
"statusText":"Bad Request",
"headers":{
"Server":[
"Apache/2.4.23 (Amazon) mod_wsgi/3.5 Python/3.4.3"
],
"Transfer-Encoding":[
"Identity"
],
"Content-Type":[
"application/json"
],
"Date":[
"Thu",
" 20 Oct 2016 13:10:04 GMT"
],
"Connection":[
"close"
],
"X-Frame-Options":[
"SAMEORIGIN"
],
"Access-Control-Allow-Origin":[
"*"
],
"Allow":[
"POST",
" OPTIONS"
]
},
"type":2,
"url":"http://www.worldimagearchive.com/rest-auth/registration/"
}
I can see how to pull out the 'status' and 'ok' values (using err.status
& err.ok
) but can't work out how to parse the "_body" section which may or may not contain errors for email, username, etc.
What do I need to do to get the value of the \"email\"
section (and test if that section exists by the way) ?
答案 0 :(得分:1)
它是JSON中的JSON。我尝试了很多东西,没有任何效果,所以这是一种非传统的方法来抓取电子邮件。
err=err.replace('"{','{').replace('}"','}'). replace(/\\/gi, '')
JSON.parse(input)._body.email;//"Enter a valid email address."