龙空技术网

前端 传来的json数组字符串 后台如何转换

小氟碳 555

前言:

眼前你们对“ajax获取的数据怎么转json数据”大体比较讲究,看官们都想要知道一些“ajax获取的数据怎么转json数据”的相关内容。那么小编在网摘上网罗了一些关于“ajax获取的数据怎么转json数据””的相关文章,希望你们能喜欢,同学们快快来学习一下吧!

前端 js

//保存

function save() {

//获取答案

var planId = $("#planId").val();

var formId = '${formId}';

var assessment = [];

$(".questionList").each(function() {

var questionId = $(this).find("input[name=questionId]").val();

var answer = $(this).find("[name='answer']").val();

assessment.push({

questionId : questionId,

formId : formId,

answer : answer,

planId : planId

});

});

$.ajax({

async : false,

type : 'post',

url : '${path}/selfAssessment/save',

data : {

'assessment' : JSON.stringify(assessment)

},

dataType : 'json',

success : function(data) {

if (data.success == true) {

$("#save").modal("show");

}

}

});

}

后端

@RequestMapping(value = "/save")

@ResponseBody

public AjaxJson save(String assessment) {

AjaxJson json = new AjaxJson();

if(StringUtils.isBlank(assessment)){

json.setSuccess(false);

return json;

}

TUser tUser = userService.getCurrentLoginUser();

JSONArray jsonArray = JSONArray.parseArray(assessment);

// 批量保存

List<TSelfAssessment> list = new ArrayList<TSelfAssessment>();

//批量更新

List<TSelfAssessment> edit_list = new ArrayList<TSelfAssessment>();

for (int i = 0; i < jsonArray.size(); i++) {

JSONObject jsonJ = jsonArray.getJSONObject(i);

Integer questionId = jsonJ.getInteger("questionId");

Integer planId = jsonJ.getInteger("planId");

Integer formId =jsonJ.getInteger("formId");

String answer = jsonJ.getString("answer");

//检查数据库是否已存在数据、

TSelfAssessment tSelfAssessment=selfAssessmentService.getselfAssessmen(planId,questionId,tUser.getUserId());

logger.info(tSelfAssessment);

if(tSelfAssessment==null){

tSelfAssessment = new TSelfAssessment();

TEvaluationPlan evaluationPlan = new TEvaluationPlan();

evaluationPlan.setId(planId);

TEvaluationForm evaluationForm=new TEvaluationForm();

evaluationForm.setId(formId);

TQuestionSubjectiveItem questionSubjective =questionSubjectiveItemService.getOne(questionId);

tSelfAssessment.setAnswer(answer);

tSelfAssessment.setQuestiontitledesc(questionSubjective.getQuestionTitledesc());

tSelfAssessment.setQuestiontitledescEn(questionSubjective.getQuestionTitledesc_en());

tSelfAssessment.setQuestionSubjective(questionSubjective);

tSelfAssessment.setEvaluationForm(evaluationForm);

tSelfAssessment.setEvaluationPlan(evaluationPlan);

tSelfAssessment.setCreateUserId(tUser.getUserId());

tSelfAssessment.setParticipantcode(tUser.getUserId());

tSelfAssessment.setCreateDate(new Date());

tSelfAssessment.setSubmitSave(CICCPEConstants.SUBMIT_SAVE_0);

//要是找不到数据 就保存到数据库

list.add(tSelfAssessment);

}else{

tSelfAssessment.setAnswer(answer);

tSelfAssessment.setUpdateDate(new Date());

tSelfAssessment.setUpdateUserId(tUser.getUserId());

edit_list.add(tSelfAssessment);

}

}

if(list.size() >0){

selfAssessmentService.saveOnly(list);

}else{

selfAssessmentService.batchUpdate(edit_list);

}

return json;

}

欢迎大家一起学习讨论

#Java# #JavaScript#

标签: #ajax获取的数据怎么转json数据