js数组转换成json数组(包含extjs的checkbox勾选项获取办法)

本文介绍了如何将JS数组转换为JSON数组,以适应后台接收,并展示了EXTJS中checkbox选中项的获取方法。通过示例代码,演示了从EXTJS网格选择模型中获取选中记录,将数据转换为正确格式的JSON数组,以便于后台使用modelList接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js数组 var exportRecords=[]; exportRecords.push(record);

{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"}

{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}

在后台直接用对应属性拼接的model并不能接收到,要先转化成json数组。


错误写法:

var json ={};

for(var i=0;i<exportRecords.length;i++){

json[i]=exportRecords[i];

}

param:

data:JSON.stringify(json);

只能得到

{"0":{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"},"1":{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}}

还是不能用modelList接收到。


正确写法:

要获得json数组的数据格式应该是:

[{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"},{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}]

完整代码如下:(包含extjs的checkbox勾选项获取办法)


function exportExcel() {
//先获取选择模型,然后从选择模型中获取选中的记录
var selections = grid.getSelectionModel().getSelection();
var exportRecords=[];
var json = [];
var sendData="";
if (selections.length > 0) {
Ext.Msg.confirm('提示', '你确认导出选中的记录吗?', function(_btn) {
                     if (_btn == 'yes') {
                         for (var i = 0; i < selections.length; i++) {
                             var record = selections[i].data;
                             exportRecords.push(record);
                         }
for(var i=0;i<exportRecords.length;i++)
{
    sendData=sendData+JSON.stringify(exportRecords[i])+",";//转成json格式字符串

sendData=sendData.substr(0,sendData.length-1);//去掉最后一个逗号,
sendData="["+sendData+"]";//拼接中括号[]
//alert(sendData);

                     if(Ext.getCmp('writeStartDate').getValue()!=null)
                         writeStartDate=Ext.getCmp('writeStartDate').getValue();
                     if(Ext.getCmp('writeEndDate').getValue()!=null)
                         writeEndDate=Ext.getCmp('writeEndDate').getValue();
                         
                         Ext.MessageBox.wait('正在导出测点Excel文档', '请稍候...');


Ext.Ajax.request({
url : 'action.ReportManage.ProjectReportAction.do?method=exportRptDailyExcel',
params : {
'data' : sendData,
'pId' : projectId,
'writeStartDate' :writeStartDate,
'writeEndDate' : writeEndDate
},
success : function(response, opts) {
Ext.MessageBox.hide();
var fp = Ext.decode(response.responseText).data;
window.open('../../../' + fp,'_self'); // 下载Excel导出文件

}
});
                   }
                 });
}else{
Ext.Msg.alert('提示', '请勾选要导出的记录');
}



}


后台:

List<RptDailyRecordData> exportRecordsList=ActionUtil.getReqContentWithList(RptDailyRecordData.class);


public class RptDailyRecordData {
private String dId;
private String id;//日报id
private String monitorDate;
private Integer mmId;
private String mmName;//监测项目
private int index;
private int issue;
private String pStatusReport;
private String path;
private String pmId;
private String pAddress;
private String writeDate;
private String writePerson;//监测人员
private String comment;//备注
private String analysis;//概要分析

//省略无参和有参的构造函数,还有getter,setter方法

}


/**
* 将前台传过来的json串转化为clazz类型的链表
*/
public static <T> List<T> getReqContentWithList(Class<T> clazz) {
try {
return (List<T>) JsonUtil.json2List(ActionContext.getRequest()
.getParameter("data"), clazz);
} catch (Exception e) {
log.error("将前台数据封装为bean对象的链表时出错", e);
throw new RuntimeException("将前台数据封装为bean对象的链表时出错");
}
}


/**
* json字符串转换为javaBean的链表

*/
@SuppressWarnings("unchecked")
public static <T> List<T> json2List(String json, Class<T> beanClass) {
try {
json = json.replaceAll("(\\d{4}-\\d{2}-\\d{2})(T)", "$1 ");
ObjectMapper objectMapper = getMapperInstance();
objectMapper.setDateFormat(myFormat);
return (List<T>) objectMapper.readValue(json,
getCollectionType(List.class, beanClass));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("json字符串转化为List对象出错");
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值