我有一个脚本从PHP接收JSON。我必须解析数据。不幸的是,我无法将JSON解析为对象并获得不同的数组。我试图验证JSON,但我也找不到错误。
代码:
$.ajax({
type: "POST",
dataType: "html",
url: "/sqldblink.php",
data: data,
success: function(data) {
var recdata=data;
console.log("Received data from listregistered:");
console.log("Server reports:" + recdata);
ListRegisteredResults(recdata);
},
error: function () {
console.log("Failed to list!");
}
});
function ListRegisteredResults(recdata) {
console.log(typeof recdata);
var data = JSON.parse(recdata);
console.log(data);
console.log(typeof data);
console.log(data.Address.length);
}
输出:
Server reports:"{\"Address\":[\"Home\",\"\",\"Home,\nHoover House 85\",\"\",\"Home\",\"\",\"\",\"\",\"\",\"\"],\"BloodGroup\":[\"o+\",\"\",\"B+\",\"B+\",\"AB+\",\"o+\",\"o-\",\"\",\"\",\"\"],\"Occupation\":[\"Vendor\",\"\",\"Carpenter\",\"Playing\",\"Nurse\",\"IT professional\",\"Engineer\",\"Doctor\",\"\",\"\"],\"Alternate\":[\"0\",\"0\",\"925\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"],\"Email\":[\"some@site.com\",\"\",\"jim@site.com\",\"\",\"\",\"\",\"arun@site.com\",\"\",\"\",\"\"],\"Mobile\":[\"90000006\",\"90000005\",\"90000005\",\"34344444\",\"902w0w05\",\"90002005\",\"900020w5\",\"90000005\",\"90002105\",\"90000005\"],\"Marital\":[\"Married\",\"Married\",\"Married\",\"Unmarried\",\"Unmarried\",\"Married\",\"Married\",\"Married\",\"Married\",\"Married\"],\"Gender\":[\"1\",\"2\",\"\",\"1\",\"1\",\"2\",\"2\",\"1\",\"1\",\"1\"],\"Age\":[\"28\",\"65\",\"35\",\"2\",\"25\",\"34\",\"31\",\"28\",\"60\",\"58\"],\"Name\":[\"Tracy Jim\",\"George Jose\",\"Jim G Mathew\",\"Cary jim\",\"Becky Mathew\",\"Cary Guy\",\"Arun Mose\",\"Tracy Kelly\",\"Dr Kim\",\"Steven Ludwig\"],\"HospitalID\":[\"3\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"16\"]}"
string
{"Address":["Home","","Home,\nHoover House 85","","Home","","","","",""],"BloodGroup":["o+","","B+","B+","AB+","o+","o-","","",""],"Occupation":["Vendor","","Carpenter","Playing","Nurse","IT professional","Engineer","Doctor","",""],"Alternate":["0","0","925","0","0","0","0","0","0","0"],"Email":["some@site.com","","jim@site.com","","","","arun@site.com","","",""],"Mobile":["90000006","90000005","90000005","34344444","902w0w05","90002005","900020w5","90000005","90002105","90000005"],"Marital":["Married","Married","Married","Unmarried","Unmarried","Married","Married","Married","Married","Married"],"Gender":["1","2","","1","1","2","2","1","1","1"],"Age":["28","65","35","2","25","34","31","28","60","58"],"Name":["Tracy Jim","George Jose","Jim G Mathew","Cary jim","Becky Mathew","Cary Guy","Arun Mose","Tracy Kelly","Dr Kim","Steven Ludwig"],"HospitalID":["3","5","6","7","8","9","10","11","12","16"]}
string
userjs/main.js:347 Uncaught TypeError: Cannot read property 'length' of undefined
at ListRegisteredResults (userjs/main.js:347)
at Object.success (userjs/main.js:295)
at i (jquery-3.2.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
at A (jquery-3.2.1.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)
在将字符串解析为JSON之后,为什么它仍然被报告为字符串?
答案 0 :(得分:2)
从您的代码中移除dataType: "html"
以及任何JSON.parse()
,它都会有效。如果没有设置错误的dataTypes,jQuery会自动解析数据,或json
$.ajax({
type: "POST",
url: "/sqldblink.php",
data: data,
success: function(data) {
var recdata=data;
console.log("Received data from listregistered:");
console.log("Server reports:" + recdata);
ListRegisteredResults(recdata);
},
error: function () {
console.log("Failed to list!");
}
});
function ListRegisteredResults(recdata) {
console.log(typeof recdata);
var data = recdata;
console.log(data);
console.log(typeof data);
console.log(data.Address.length);
}
答案 1 :(得分:2)
你JSON无效。 <{1}}位置\n
打破了它,你需要逃脱它。
28
应该是
"Address":["Home","","Home,\nHoover House 85"
如果你想保留"Address":["Home","","Home,\\nHoover House 85"
。
\n
答案 2 :(得分:0)
这只是因为dataType: "html"
而发生的,您发送的是您的帖子请求。因此,您使用'\'获得的响应使JSON.parse()
无法处理它们。
从发布请求中删除dataType
然后尝试,然后您将获得正确的回复和所需的结果。
代码:
$.ajax({
type: "POST",
url: "/sqldblink.php",
data: data,
success: function(data) {
var recdata=data;
console.log("Received data from listregistered:");
console.log("Server reports:" + recdata);
ListRegisteredResults(recdata);
},
error: function () {
console.log("Failed to list!");
}
});
function ListRegisteredResults(recdata) {
console.log(typeof recdata);
var data = JSON.parse(recdata);
console.log(data);
console.log(typeof data);
console.log(data.Address.length);
}