有时候前台传递到后台数据多了,可以在前台把数据拼接成json字符串传递到后台,后台可以在转为list对象集合,会简单很多。

前台拼接json字符串代码(这里是循环遍历获取table表格中的标签值传递,如下图表格中的数据)

表格中数据代码(部分,以为数据条数可以添加,所以这里只显示了一行的代码)

拼接表格中的所有数据为json格式字符串代码(因为json格式代码需要加 " 号,拼接时需要注意一下):

var str = "[";
$("#lesson_body").each(function(){
	$(this).find('tr').each(function(){
		str += '{"id":"'+$(this).find(".id").val()+'",';
		str += '"startdata":"'+$(this).find(".startdata").val()+'",';
		str += '"finishtime":"'+$(this).find(".finishtime").val()+'",';
		str += '"curriculum1":"'+$(this).find(".curriculum").val()+'",';
		str += '"teacher1":"'+$(this).find(".teacher").val()+'"},';
	})
});
str = str.substring(0, str.length-1)+"]";
$.post("${pageContext.request.contextPath }/curriculumPlan/save1", {data: str, tbClassId: tbClassId}, function(data){
	if(data.success){
		layer.msg("保存成功",{offset:'rb'});
	}else{
		layer.msg("保存失败",{offset:'rb'});
	}
})

后台接收为一个string类型字符串,之后转为list对象集合:

首先需要引入json依赖:

<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.9</version>
</dependency>

转换代码CurriculumPlanForm为对象实体类:

ObjectMapper mapper = new ObjectMapper();
    //data为字符串json数据
	List<CurriculumPlanForm> list =     
		mapper.readValue(data,new TypeReference<List<CurriculumPlanForm>>() { });

 

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2134cb94 * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance * fix ci_static_analysis_clang (ci_clang_tidy) * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance 8 天前
6057b31d * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * Use ubuntu-latest image to run Valgrind (#4575) * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Ubuntu image to run valgrind * Use Clang image to run iwyu (#4574) * :wrench: use Clang image to run iwyu * :wrench: use Clang image to run iwyu * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :art: format code * :hammer: clean up 10 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐