如何将gridview数据转换为Json? [英] How to convert gridview data to Json?
问题描述
我有一个任务,我必须使用数据库中的数据创建一个GridView,并将Gridview中的信息导出到Pdf和Json。
完成与PDF部分,但现在我坚持在JSON部分,我不知道如何实现它。
var json = new 。JavaScriptSerializer()序列化(GridViewStudents.Rows);
Response.ContentType =application / json; charset = utf-8;
Response.AppendHeader(content-disposition,attachment; filename = Students.txt);
Response.Write(json);
Response.End();
这就是我想要做的。我需要先将数据从
-
Gridview转换为Json
-
将数据导出到某个文件,如txt文件,它将显示可转换的数据。
我已经写过的代码给出了错误,并说明了哪些状态:
在序列化类型为System.Web.UI.WebControls.GridViewRow的对象时检测到循环引用
任何人都可以帮助我找到合适的解决方案。
谢谢。
包含这些文件
< script src =〜/ Scripts / jquery。 base64.js>< /脚本>
< script src =〜/ Scripts / tableExport.js>< / script>
< script src =〜/ Scripts / jspdf / libs / base64.js>< / script>
< script src =〜/ Scripts / jspdf / jspdf.js>< / script>
< script src =〜/ Scripts / jspdf / FileSaver.js>< / script>
< script src =〜/ Scripts / jspdf / jspdf.plugin.cell.js>< / script>
然后将这些函数添加到您的脚本中
< $($'$'$>'$'$>'$'$> $ $ $ $ $ $ $'$'$> ),
cellWidth = 35,
rowCount = 0,
cellContents,
// leftMargin = 2,
// topMargin = 12,
// topMarginTable = 55,
// headerRowHeight = 13,
// rowHeight = 9,
leftMargin = 10,
topMargin = 15,
topMarginTable = 5,
headerRowHeight = 13,
rowHeight = 13,
l = {
方向:'l',
单位:'mm',
格式: 'a3',
compress:true,
fontSize:8,
lineHeight:1,
autoSize:false,
printHeaders:true
};
var doc = new jsPDF(l,'','','');
doc.setProperties({
title:'Test PDF Document',
subject:'This is the subject',
author:'author',
关键字:'generated,javascript,web 2.0,ajax',
creator:'author'
});
doc.cellInitialize();
$ .each(table1,function(i,row){
rowCount ++;
$ .each(row,function(j,cellContent ){
if(rowCount == 1){
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin,topMargin ,cellWidth,headerRowHeight,cellContent,i)
}
else if(rowCount == 2){
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin,topMargin,cellWidth,rowHeight,cellContent,i);
}
else {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin,topMargin,cellWidth,rowHeight,cellContent,i);
}
})
} )
doc.save('sample Report.pdf');
}
函数tableToJson(table){
var data = [];
//第一行需要标题
var headers = []; (table.rows [0] .cells [i] .innerHTML!=
for(var i = 0; i< table.rows [0] .cells.length; i ++) ){
headers [i] = table.rows [0] .cells [i] .innerText.toLowerCase()。replace(/ / gi,'');
//通过单元格
(var i = 1; i
var rowData = {};
for(var j = 1; j
}
data.push(rowData);
}
返回数据;
}
I have a task in which i have to create a gridview with data from database and export the information from the Gridview to Pdf and also Json.
I am done with PDF part but now i am stuck at the Json part and i have no idea how to achieve it.
var json = new JavaScriptSerializer().Serialize(GridViewStudents.Rows);
Response.ContentType = "application/json; charset=utf-8";
Response.AppendHeader("content-disposition", "attachment;filename=Students.txt");
Response.Write(json);
Response.End();
This is what i was trying to do. I need to first convert the data from
Gridview to Json
Export the data to some file like txt file which will show the convertable data.
The code i have already written gives and error which states
A circular reference was detected while serializing an object of type 'System.Web.UI.WebControls.GridViewRow'
Can any one help me through this and find the appropriate solution.
Thank you.
include these files
<script src="~/Scripts/jquery.base64.js"></script>
<script src="~/Scripts/tableExport.js"></script>
<script src="~/Scripts/jspdf/libs/base64.js"></script>
<script src="~/Scripts/jspdf/jspdf.js"></script>
<script src="~/Scripts/jspdf/FileSaver.js"></script>
<script src="~/Scripts/jspdf/jspdf.plugin.cell.js"></script>
And then add these functions to your scripts
function ExportTpGridtoPDF(divid) {
var table1 =
tableToJson($('#' + divid + ' .grid-table').get(0)),
cellWidth = 35,
rowCount = 0,
cellContents,
//leftMargin = 2,
//topMargin = 12,
//topMarginTable = 55,
//headerRowHeight = 13,
//rowHeight = 9,
leftMargin = 10,
topMargin = 15,
topMarginTable = 5,
headerRowHeight = 13,
rowHeight = 13,
l = {
orientation: 'l',
unit: 'mm',
format: 'a3',
compress: true,
fontSize: 8,
lineHeight: 1,
autoSize: false,
printHeaders: true
};
var doc = new jsPDF(l, '', '', '');
doc.setProperties({
title: 'Test PDF Document',
subject: 'This is the subject',
author: 'author',
keywords: 'generated, javascript, web 2.0, ajax',
creator: 'author'
});
doc.cellInitialize();
$.each(table1, function (i, row) {
rowCount++;
$.each(row, function (j, cellContent) {
if (rowCount == 1) {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
}
else if (rowCount == 2) {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
}
else {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
}
})
})
doc.save('sample Report.pdf');
}
function tableToJson(table) {
var data = [];
// first row needs to be headers
var headers = [];
for (var i = 0; i < table.rows[0].cells.length; i++) {
if (table.rows[0].cells[i].innerHTML != "") {
headers[i] = table.rows[0].cells[i].innerText.toLowerCase().replace(/ /gi, '');
}
}
// go through cells
for (var i = 1; i < table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j = 1; j < tableRow.cells.length; j++) {
rowData[headers[j]] = tableRow.cells[j].innerText;
}
data.push(rowData);
}
return data;
}
这篇关于如何将gridview数据转换为Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!