SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data? 400 报错
ssm的web项目 controller的action方法如下:
@RequestMapping(value = "/toMapPage/{stageId}", method = RequestMethod.GET)
public String toMapPage(@PathVariable("stageId") String stageId, HttpServletRequest request, Model model) {
List<Group> groups = stageService.getAginstMap(stageId);
model.addAttribute("groups", new Gson().toJson(groups));
model.addAttribute("stageId", stageId);
return "aginstMaps/doubleOut";
}
页面中的js想要获取model中的json数据,获取之后在转json对象时报错:
$(function(){
var groups_json = ${groups};
var groups = JSON.parse(groups_json);
/* var groups=eval("("+groups_json+")"); */
$.each(groups,function(idx,item){
alert("group:"+item.group);
});
});
debug实际上获取的json如下:
var groups_json = [{"group":"A组","roundMap":{"L2":[{"Id":"dd50c72f37434f7eb9b0cdc06058e4db","A":{"str":"2","seed":false},"B":{"str":"4","seed":false},"status":0},{"Id":"713cac240a1b4735be5bfb0f068cd5f7","A":{"str":"6","seed":false},"B":{"str":"8","seed":false},"status":0},{"Id":"1d63c6a801344324b3a407ef906e0423","A":{"str":"2","seed":false},"B":{"str":"3","seed":false},"status":0},{"Id":"34f38724b64f4c0fb77f67eb5afef05e","A":{"str":"6","seed":false},"B":{"str":"7","seed":false},"status":0}],"L3":[{"Id":"8d9b927fc1344491909ebc59b01a6b38","A":{"str":"2","seed":false},"B":{"str":"6","seed":false},"status":0}],"END4":[{"Id":"baf97f8320da40f4b78e30d257bf8a4f","A":{"str":"1","seed":false},"B":{"str":"2","seed":false},"status":0}],"W1":[{"Id":"da5f179f414c436c985e152efc9a651d","A":{"str":"1","seed":false},"B":{"str":"2","seed":false},"status":0},{"Id":"8f1ee2497b984113bd15c718420061f0","A":{"str":"3","seed":false},"B":{"str":"4","seed":false},"status":0},{"Id":"2d1a323838064561af95bad070d658a2","A":{"str":"5","seed":false},"B":{"str":"6","seed":false},"status":0},{"Id":"56d36df8d57143ff8435e422d6e72082","A":{"str":"7","seed":false},"B":{"str":"8","seed":false},"status":0}],"W2":[{"Id":"1bf495d8d6da41ba82d32dd3d1ea52b2","A":{"str":"1","seed":false},"B":{"str":"3","seed":false},"status":0},{"Id":"a782f2266b2f465a9d3a705667528a6b","A":{"str":"5","seed":false},"B":{"str":"7","seed":false},"status":0}],"W3":[{"Id":"dacabfba4e3c438e8231535e8c1181f8","A":{"str":"1","seed":false},"B":{"str":"5","seed":false},"status":0}]}}];
var groups = JSON.parse(groups_json);
请问为什么会报这个错误,json是用Gson转换的,格式不可能不标准。
我想在页面跳转的同时将json字符串传递给页面,并在页面显示数据。
另外,网上说可以使用ajax,如果这样浏览器就需要发送两次请求一次是跳转页面并将stageid通过action传递给页面中,再在页面中调用ajax把这个stageid专递给另外一个action获取数据。我觉着这样两次请求有点啰嗦。
自己后来好像是解决了,记不清了,这是当时写的一篇笔记:https://my.oschina.net/iyinghui/blog/1486840
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。