开发者问题收集

JSON.parse:无法在代码中发现错误

2018-03-20
147

我有一个脚本从 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\":[\"[email protected]\",\"\",\"[email protected]\",\"\",\"\",\"\",\"[email protected]\",\"\",\"\",\"\"],\"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":["[email protected]","","[email protected]","","","","[email protected]","","",""],"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 后,为什么它仍被报告为字符串?

3个回答

从代码中删除 dataType: "html" 以及任何 JSON.parse() ,它就会正常工作。如果您没有设置错误的数据类型,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);
}
baao
2018-03-20

您的 JSON 无效。位置 28 处的 \n 破坏了它,您需要对其进行转义。

"Address":["Home","","Home,\nHoover House 85"

应为

"Address":["Home","","Home,\\nHoover House 85"

如果您想保留该 \n

let jsonString = `{"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":["[email protected]","","[email protected]","","","","[email protected]","","",""],"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"]}`;

let json = JSON.parse(jsonString);
console.log(json.Address);
Zenoo
2018-03-20

发生这种情况只是因为 dataType: "html" ,您正在发送 post 请求。因此,您获得的响应带有 '\',因此 JSON.parse() 无法处理它们。 从 post 请求中删除 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);
}
Gurpreet Singh
2018-03-20