开发者社区> 问答> 正文

SyntaxError: JSON.parse: unexpected char?报错

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获取数据。我觉着这样两次请求有点啰嗦。

展开
收起
优选2 2020-06-04 21:18:15 828 0
1 条回答
写回答
取消 提交回答
  • 自己后来好像是解决了,记不清了,这是当时写的一篇笔记:https://my.oschina.net/iyinghui/blog/1486840

    2020-06-05 11:51:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载