我必须管理一个给我一个JSON的ajax文件的响应,如下所示:
{
"action": "register",
"error": "	Please fill in all required fields. ",
"newScreenName": "",
"invalidCredentials": "",
"missingFields": [ "email", "screenName", "passwd", "passwd2",
"fname", "lname", "profile", "zip", "country", ],
"errorFields": []
}
我有两个选择:或者我找到一种方法在obj中推送它,这样我就可以得到我需要的数据:Obj.action,在这种情况下返回“register”。如果这是不可能的方式将它与正则表达式匹配,我可以在[]之间找到所有,所以我可以知道我有什么missingFields和errorFields。 我正在使用javascript
我用它来做出回应: var text = $('*')。text()。trim()
给我这个:
{\"action\": \"register\",
\"error\": \"	Please fill in all required fields. \",
\"newScreenName\": \"\",
\"invalidCredentials\": \"\",
\"missingFields\": [\"email\", \"screenName\", \"passwd\", \"passwd2\", \"fname\", \"lname\", \"profile\", \"zip\", \"country\", ],
\"errorFields\": []}
所以我用它来删除\:text = text.replace(/“/ g,”'“)
谢谢
答案 0 :(得分:3)
这个神奇的功能恰好是JSON.parse()
var data = JSON.parse(YOUR_JSON_HERE);
var action = data.action; //register
var missingFields = data.missingFields // [ "email", "screenName", "passwd", "passwd2", "fname", "lname", "profile", "zip", "country", ]