getmany返回值 gjson_后端返回值以json的格式返回,前端以json格式接收

以随便一个类为例子:这个例子是查询企业主营类别前5事项

一、以json数组的格式返回到前端中

(1)后端将结果绑定到param中,然后将结果以为json数组的格式返回到前端

/*** 查询企业主营类别前5事项

*@paramrequest

*@paramresponse

*@paramconfig

*@throwsException

*@authorhongxy

* 2017年6月1日下午2:21:14*/

public voidgetEnterpriseMainCategory(HttpServletRequest request,

HttpServletResponse response, ServletConfig config)throwsException {

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

Map param= newHashMap();

PrintWriter wirte= null;//获得企业名称

String custName = RequestUtil.getString(request, "companyName");//判断企业名称是否为空

if(StringUtils.isBlank(custName)) {

param.put("status", "400");

param.put("desc", "企业名称为空!");

}else{

workService= newWorkServiceImpl();//查询是否存在该企业

Map enterpriseInfo =workService.getEnterpriseInfoByCustName(custName);//不存在该企业

if (enterpriseInfo == null) {

param.put("status", "400");

param.put("desc", "企业名称不存在!");

}else {//存在该企业,查询企业主营类别前5事项//根据企业名称查询出该企业近一年的已办事项

String approveTypeList =workService.getEnterpriseWorksInfoByCustName(custName);//根据行业类别查询企业名称

String custNameListByIndustry = workService.getEnterpriseNameByIndustry((String) enterpriseInfo.get("INDUSTRY"));//查询企业主营类别前5事项

List mainProjectList =workService.getApproveInfoList(custNameListByIndustry,approveTypeList);

param.put("status", "200");

param.put("desc", "处理成功");

param.put("data", mainProjectList);

}

}//声明JSONArray对象并输入JSON字符串

JSONArray array =JSONArray.fromObject(param);

wirte=response.getWriter();

wirte.print(array);

}

(2)前端先将接受到的数据转换成json格式,不然就不能获取里面的值了,因为Ajax返回的值默认是字符串类型

将接收到的值转换成json格式的核心代码:(具体的取值可以输出到前端控制台,这样方便取值)

var msg=jQuery.parseJSON(msg);

$.ajax({

url:'${path.appMain}?service=work&func=getEnterpriseMainCategory',

async:false,

type:'POST',

data: {

companyName:companyName

},

success:function(msg){var msg=jQuery.parseJSON(msg);var mainProjectList = "";

mainProjectList+="

与您的“主营项目类别”相同的企业,办理最多的事项有:"

for (var i = 0; i < msg[0].data.length; i++) {

mainProjectList+= "

"+(i+1) + "." + msg[0].data[i].approveName +

"

";

}

$('#mainProjectList').html(mainProjectList);

}

});

二、以json的格式返回到前端中(常用)

(1)后端将结果绑定到data中,然后将结果以为json的格式返回到前端

/*** 查询企业主营类别前5事项

*@paramrequest

*@paramresponse

*@paramconfig

*@throwsException

*@authorhongxy

* 2017年6月1日下午2:21:14*/

public voidgetEnterpriseMainCategory(HttpServletRequest request,

HttpServletResponse response, ServletConfig config)throwsException {

request.setCharacterEncoding("UTF-8");

JSONObject json= newJSONObject();

Map param= newHashMap();//获得企业名称

String custName = RequestUtil.getString(request, "companyName");//判断企业名称是否为空

if(StringUtils.isBlank(custName)) {

json.put("status", "400");

json.put("desc", "企业名称为空!");

}else{

workService= newWorkServiceImpl();//查询是否存在该企业

Map enterpriseInfo =workService.getEnterpriseInfoByCustName(custName);//不存在该企业

if (enterpriseInfo == null) {

json.put("status", "400");

json.put("desc", "企业名称不存在!");

}else {//存在该企业,查询企业主营类别前5事项//根据企业名称查询出该企业近一年的已办事项

String approveTypeList =workService.getEnterpriseWorksInfoByCustName(custName);//根据行业类别查询企业名称

String custNameListByIndustry = workService.getEnterpriseNameByIndustry((String) enterpriseInfo.get("INDUSTRY"));//查询企业主营类别前5事项

List mainProjectList =workService.getApproveInfoList(custNameListByIndustry,approveTypeList);

json.put("status", "200");

json.put("desc", "处理成功");

json.put("data", mainProjectList);

}

}//响应请求

SysInfo.responseJsonMsg(response, json.toString());

}

(2)前端接收json数据并在前端进行显示

$.ajax({

url:'${path.appMain}?service=work&func=getEnterpriseMainCategory',

async:false,

type:'POST',

data: {

companyName:companyName

},

success: function(msg){

var mainProjectList= "";

mainProjectList+="

与您的“主营项目类别”相同的企业,办理最多的事项有:"

if(msg.data.length == 0){

mainProjectList+= "

查找不到相关的事项!";

}else{for (var i = 0; i < msg.data.length; i++) {

mainProjectList+= "

"+(i+1) + "." + msg.data[i].approveName +

"

";

}

}

$('#mainProjectList').html(mainProjectList);

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值