TianYi3G2013 2014年01月15日
DataTableToObject[][]_To_Json_问题请教


//我想要的最终Json结果为:
{
"TicketNumPercent" : [["景区01", 10], ["景区11", 20], ["景区21", 30], ["景区31", 40], ["景区41", 50]],
"SalePricePercent" : [["景区01", 0], ["景区11", 1], ["景区21", 2], ["景区31", 3], ["景区41", 4]],
"CostPricePercent" : [["景区01", 7], ["景区11", 14], ["景区21", 21], ["景区31", 28], ["景区41", 35]],
"CommissionAmountPercent" :[["景区01", 7], ["景区11", 14], ["景区21", 21], ["景区31", 28], ["景区41", 35]],
"ProfitAmountPercent" : [["景区01", 7], ["景区11", 14], ["景区21", 21], ["景区31", 28], ["景区41", 35]]
}



//DataTable:

System.Data.DataTable dTab = new System.Data.DataTable();

dTab.Columns.Add("Scence");
dTab.Columns.Add("TicketNum");
dTab.Columns.Add("TicketNumPercent");
dTab.Columns.Add("SalePrice");
dTab.Columns.Add("SalePricePercent");
dTab.Columns.Add("CostPrice");
dTab.Columns.Add("CostPricePercent");
dTab.Columns.Add("CommissionAmount");
dTab.Columns.Add("CommissionAmountPercent");
dTab.Columns.Add("ProfitAmount");
dTab.Columns.Add("ProfitAmountPercent");

System.Data.DataRow dr = dTab.NewRow();
for (int i = 0; i < 10; i++)
{
dr = dTab.NewRow();
dr["Scence"] = "景区" + i + 1;
dr["TicketNum"] = i;
dr["TicketNumPercent"] = 10 * (i + 1);
dr["SalePrice"] = i;
dr["SalePricePercent"] = i;
dr["CostPrice"] = 7 * (i + 1);
dr["CostPricePercent"] = 7 * (i + 1);
dr["CommissionAmount"] = 7 * (i + 1);
dr["CommissionAmountPercent"] = 7 * (i + 1);
dr["ProfitAmount"] = 7 * (i + 1);
dr["ProfitAmountPercent"] = 7 * (i + 1);
dTab.Rows.Add(dr);
}



//用为序列化为json的类:
public class ReportSceneClass2
{
[JsonProperty("TicketNum")]
public object[][] TicketNum { get; set; }

[JsonProperty("SalePrice")]
public object[][] SalePrice { get; set; }

[JsonProperty("CostPrice")]
public object[][] CostPrice { get; set; }

[JsonProperty("CommissionAmount")]
public object[][] CommissionAmount { get; set; }

[JsonProperty("ProfitAmount")]
public object[][] ProfitAmount { get; set; }
}


//在抽取DataTable中对应的行,列时不会处理了:。。。
public static ReportSceneClass2 ColumnsToObject(DataTable dt)
{
int arrayLength = dt.Rows.Count;
ReportSceneClass2 objReportSceneClass2 = new ReportSceneClass2();

objReportSceneClass2.TicketNum = new object[arrayLength][];

//【问题】这里怎样从dt中抽取对应的行,列赋值给ReportSceneClass2类的object[][] ?

for (int index = 0; index < arrayLength; index++)
{



}

return objReportSceneClass2;
}



如果上面的转换方式显得太笨拙了,还有其它更好的方法么?请指教!
...全文
50 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告