【发布时间】:2021-08-31 19:25:54
【问题描述】:
我查看了所有与我类似的问题,但在我的代码或 JSON 字符串中找不到任何明显的错误。
脚本向 Web 服务发送请求,该服务返回 JSON 格式的字符串。不知何故,与调试相关的 html 被添加到 JSON 字符串的末尾,因此我使用 string.slice 方法将其删除,仅返回左方括号和右方括号之间的内容。当我输出切片时,一切都在那里。当我在 jsonlint 中检查结果切片是否有正确的 JSON 格式时,它通过了。但是,当我尝试解析字符串并创建解析数据的警报时,我收到一个错误:XMLHttpRequest.request.onload 处的 Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse)
这是切片后的 JSON 字符串,直接取自 jsonlint 输出:
[{
"user_id": "23",
"name": "Steven Marsden",
"email": "naturevet1@gmail.com",
"password": "$2y$10$FjDZAugd2Mj9tw.z8U1mWexLF0vkD7jnB5xusGyClDUXZwQC6u9iy",
"address": "2639 Valley Rdg Rd, Blairmore, AB T0K 0E0, Canada",
"lat": "49.617134",
"lng": "-114.388893",
"created_at": "2021-06-06 18:00:20",
"updated_at": "2021-06-06 18:00:20",
"services": [{
"u_s_id": "2",
"user_id": "23",
"mode": "seek",
"service": "pet sitting",
"details": "I have a dragon",
"img_file": null
}, {
"u_s_id": "3",
"user_id": "23",
"mode": "offer",
"service": "pet sitting",
"details": "I love dragons",
"img_file": null
}, {
"u_s_id": "7",
"user_id": "23",
"mode": "offer",
"service": "pet foster home",
"details": "Is it okay if my dragon eats your pet?",
"img_file": null
}],
"color": "red"
}]
这是请求字符串并尝试解析它的 javascript:
var url = 'http://localhost/helpinghands/public/index.php/services/getUsers';//Gets all users, complete with services offered and the appropriate marker color
var request = new XMLHttpRequest();
request.open('GET',url);
request.onload = function(){
if(request.status === 200){
var resp = request.responseText;
var slice = resp.slice(resp.indexOf('['),resp.indexOf(']')+1);//Cleaves any extraneous info from end of string to avoid JSON parse errors
var data = JSON.parse(slice);
//var userLat = Number(data[0].lat);
alert(data);
//var userLong = Number(data.lng);
//const loc = new point(userLat,userLong);//This is the location from the database for that user
//create marker object for loc by passing Marker constructor references to the loc and map objects, which are assigned to position and map variables, respectively
//const marker = new google.maps.Marker({map: map,position: loc, title: data.name});
}
};
谁能帮帮我?
这是来自网络服务的输出:
[{
"user_id": "23",
"name": "Steven Marsden",
"email": "naturevet1@gmail.com",
"password": "$2y$10$FjDZAugd2Mj9tw.z8U1mWexLF0vkD7jnB5xusGyClDUXZwQC6u9iy",
"address": "2639 Valley Rdg Rd, Blairmore, AB T0K 0E0, Canada",
"lat": "49.617134",
"lng": "-114.388893",
"created_at": "2021-06-06 18:00:20",
"updated_at": "2021-06-06 18:00:20",
"services": [{
"u_s_id": "2",
"user_id": "23",
"mode": "seek",
"service": "pet sitting",
"details": "I have a dragon",
"img_file": null
}, {
"u_s_id": "3",
"user_id": "23",
"mode": "offer",
"service": "pet sitting",
"details": "I love dragons",
"img_file": null
}, {
"u_s_id": "7",
"user_id": "23",
"mode": "offer",
"service": "pet foster home",
"details": "Is it okay if my dragon eats your pet?",
"img_file": null
}],
"color": "red"
}]
【问题讨论】:
-
这是在做什么???
var slice = resp.slice(resp.indexOf('['),resp.indexOf(']')+1) -
您只需发布原始回复
http://localhost/helpinghands/public/index.php/services/getUsers,如浏览器的网络标签所示。 -
从不使用警报。如果您想知道某些东西是什么样子,请使用
console.log并阅读您的开发工具控制台。警报是一个古老的、过时的功能,您和其他任何人都不再与它有任何业务往来。此外,现代 JS 有fetch(),绝对是 use that instead(略少,但仍然非常多)古老的 XMLHttpRequest。 -
Web 服务的输出与您所说的切片返回的内容完全相同。切片将返回带有无效 JSON 的子集
-
尝试使用字符串操作“修复”JSON 是一个非常糟糕的主意。解析整个事情,然后你正在处理一个 JavaScript 对象,你可以做任何你想做的事情。
标签: javascript json parsing