前台传json数组,后台接收处理

这里前台传过来的json数据格式是这样的
[
	{
		"type": 0,
		"stem": "123",
		"deletionFlag": 1,
		"choice": ["A"],
		"postId": "123",
		"solution":["A", "B"],
		"score": 12.12,
		"materialIds": "123"
	},
	{
		"type": 1,
		"stem": "234",
		"deletionFlag": 1,
		"choice": ["A", "B", "C"],
		"postId": "234",
		"solution": ["A", "B"],
		"score": 12.23,
		"materialIds": "234"
	},
	{
		"type": 2,
		"stem": "345",
		"deletionFlag": 1,
		"choice": ["A", "B"],
		"postId": "345",
		"solution":["A", "B"],
		"score": 12.34,
		"materialIds": "345"
	}
]
  • 这里如果后台直接用List来接收是不行的,因为choice和solution的数据也还是一个数组,直接取会报错
  • 思考:因为是json格式是key/value,可以用List来装,之后通过key取出对应类型的值
    //保存
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public APIResponseBean saveHomeWork(@RequestBody List> homeworklist) {

        if (homeworklist == null) {
            throw new GlobalException(Constants.HTTP_UNPROCESSABLE_ENTITY, "提交内容参数错误。");
        }

        List saveList = new ArrayList<>();

        for (Map homeworkBeanMap : homeworklist) {
            //测试
			// System.out.println(homeworkBean);
			
            //获取id 根据id进行判断是添加还是修改
            String id = (String) homeworkBeanMap.get("id");
            
            /* 提交答案 */
            ArrayList solutionList = (ArrayList) homeworkBeanMap.get("solution");
            String solution = String.valueOf(solutionList);
            .............................省略
        }

最好是创建bean进行操作,这样性能更高

你可能感兴趣的:(前台传json数组,后台接收处理)