在做考试系统的笔记本功能,需要前端传过来对象数组
将对象数组转为json格式在传到后端,后端用String接收,然后转为json对象,在获取其中的对象数组,再遍历数组即可
var markQuestionJson = JSON.stringify(this.markQuestion); //把对象数组转成json字符串
this.$axios({
url: '/api/markQuestion',
method: 'post',
data: {
markQuestionList: markQuestionJson //标记题目
}
}).then(res => {
console.log(res);
})
springboot代码,需要有对应的实体类
@Data
public class MarkQuestion {
private long studentId;
private int questionType;
private int examCode;
private int questionId;
}
pom需要添加fastJson依赖,这个maven仓库自己去找一个,简单的,
markQuestionJson就是前端传过来的json字符串,getJSONArray("markQuestionList")就是获取json中的对象数组
@RestController
public class MarkQuestionController {
@Resource
MarkQuestionServiceImpl markQuestionService;
@PostMapping("/markQuestion")
public ApiResult markQuestion(@RequestBody String markQuestionJson){
System.out.println(markQuestionJson);
JSONObject jsonObject = JSONObject.parseObject(markQuestionJson);
List<MarkQuestion> markQuestionList = JSONObject.parseArray(jsonObject.getJSONArray("markQuestionList").toJSONString(), MarkQuestion.class);
for (MarkQuestion markQuestion:markQuestionList
) {
markQuestionService.addMarkQuestion(markQuestion);
}
return ApiResultHandler.buildApiResult(200,"保存错题成功",null);
}
}
这样就搞定了