JSON.toJSONString(实体类)和JSON.parseObject(String, xxx.class)和JSON.parseArray(JSONArray,xxx.class )

/**
	 * 新增学生
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@PostMapping("/create")
	public JSONObject create(HttpServletRequest request) {
		// 学生基本信息
		Student student = new Student();
		student.setStu1stName(request.getParameter("stu1stName"));
		student.setStu2ndName(request.getParameter("stu2ndName"));
		student.setGender(Integer.valueOf(request.getParameter("gender")));
		student.setStuName(request.getParameter("stu1stName") + "" + request.getParameter("stu2ndName"));
		Nation nation = new Nation();
		nation.setCode(request.getParameter("nationCode"));
		student.setNation(nation);

		// 学生学籍信息
		com.moofen.core.entity.sch.master.Student stu = new com.moofen.core.entity.sch.master.Student();
		stu.setSchCode(Long.valueOf(request.getParameter("schCode")));
		String examNo = request.getParameter("examNo");
		if (!examNo.isEmpty()) {
			stu.setExamNo(request.getParameter("examNo"));
		}
		String stuNo = request.getParameter("stuNo");
		if (!stuNo.isEmpty()) {
			stu.setStuNo(Long.valueOf(request.getParameter("stuNo")));
		}
		stu.setStuAlias(request.getParameter("stuAlias"));

		String str = request.getParameter("stuTag");
		List stuTags = new ArrayList();
		if (!StringUtils.isEmpty(str)) {
			String[] stuTag = str.split(",");
			for (int i = 0; i < stuTag.length; i++) {
				String[] str1 = stuTag[i].split("、");
				StudentTag tag = new StudentTag();
				tag.setSchCode(new Long(request.getParameter("schCode")));
				tag.setTagCode(new Long(str1[0]));
				tag.setTagName(str1[1]);
				stuTags.add(tag);
			}
		}
		stu.setStuTags(stuTags);

		Clazz adminClazz = new Clazz();
		adminClazz.setClzCode(Long.valueOf(request.getParameter("adminClazz")));
		stu.setAdminClazz(adminClazz);
		stu.setSchStuCode(request.getParameter("schStuCode"));

		// 选课班
		String obj = request.getParameter("selection");
		Object obj2 = JSON.parseObject("{" + obj + "}");
		JSONObject obj1 = (JSONObject) obj2;
		List stuClzList = JSON.parseArray(obj1.getString("setdate"),
				StudentClazzDecompression.class);
		JSONArray stuClsArr = getJSONArrayByList(stuClzList);
		Map params = new HashMap();
		params.put("schCode", request.getParameter("schCode"));
		params.put("bsdStudent", JSON.toJSONString(student));
		params.put("smiStudent", JSON.toJSONString(stu));
		params.put("studentClazzes", stuClsArr);
		// return null;
		return studentService.create(params);
	}
/**
	 * 新增学生
	 * 
	 * @param JSON
	 *            body 学生数据
	 * @return BaseResult 执行结果
	 */
	@PostMapping("/createComplex")
	public BaseResult createComplex(@RequestParam(name = "schCode") Long schCode,
			@RequestParam(name = "bsdStudent") String bsdStudentStr,
			@RequestParam(name = "smiStudent") String smiStudentStr,
			@RequestParam(name = "studentClazzes") String studentClazzStrs) {
		
		com.moofen.core.entity.biz.sch.Student bsdStudent = JSON.parseObject(bsdStudentStr,
				com.moofen.core.entity.biz.sch.Student.class);
		com.moofen.core.entity.sch.master.Student smiStudent = JSON.parseObject(smiStudentStr,
				com.moofen.core.entity.sch.master.Student.class);
		
		List studentClazzs = JSON.parseArray(studentClazzStrs,
				com.moofen.core.entity.sch.master.StudentClazzDecompression.class);

		return studentSPService.createStudent(schCode, bsdStudent, smiStudent, studentClazzs);

	}
	public static JSONArray getJSONArrayByList(List list) {
		JSONArray jsonArray = new JSONArray();
		if (list == null || list.isEmpty()) {
			return jsonArray;
		}

		for (Object object : list) {
			jsonArray.add(object);
		}
		return jsonArray;
	}

 

你可能感兴趣的:(java)