//我想要的最终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的类】:
[JsonProperty(
"TicketNumPercent"
)]
public
object
[][] TicketNumPercent {
get
;
set
; }
[JsonProperty(
"SalePricePercent"
)]
public
object
[][] SalePricePercent {
get
;
set
; }
[JsonProperty(
"CostPricePercent"
)]
public
object
[][] CostPricePercent {
get
;
set
; }
[JsonProperty(
"CommissionAmountPercent"
)]
public
object
[][] CommissionAmountPercent {
get
;
set
; }
[JsonProperty(
"ProfitAmountPercent"
)]
public
object
[][] ProfitAmountPercent {
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; }
如果上面的转换方式显得太笨拙了,还有其它更好的方法么?请指教!
var dt = new DataTable(); dt.Columns.Add("c"); dt.Columns.Add("col1"); dt.Columns.Add("col2"); dt.Columns.Add("col3"); dt.Rows.Add("A", 1, 2, 3); dt.Rows.Add("B", 4, 5, 6); dt.Rows.Add("C", 7, 8, 9); dt.Rows.Add("D", 10, 11, 12); var dataList = dt.Rows.Cast<DataRow>().Select(x => new { TicketNumPercent = new[] { x["c"], x["col1"] }, SalePricePercent = new[] { x["c"], x["col2"] }, CostPricePercent = new[] { x["c"], x["col3"] } }).ToList(); var jsonModel=new { TicketNumPercent = dataList.Select(x => x.TicketNumPercent), SalePricePercent = dataList.Select(x => x.SalePricePercent), CostPricePercent = dataList.Select(x => x.CostPricePercent) }; //下面的步骤,序列化jsonModel,you know? 再优化下,可以提取为公共方法!
Thanks you very much!But has some problem,the json result is:
{"TicketNumPercent":[["景区01","10"],["景区11","20"],["景区21","30"],["景区31","40"],["景区41","50"],["景区51","60"],["景区61","70"],["景区71","80"],["景区81","90"],["景区91","100"]],"SalePricePercent":[["景区01","0"],["景区11","1"],["景区21","2"],["景区31","3"],["景区41","4"],["景区51","5"],["景区61","6"],["景区71","7"],["景区81","8"],["景区91","9"]],"CostPricePercent":[["景区01","7"],["景区11","14"],["景区21","21"],["景区31","28"],["景区41","35"],["景区51","42"],["景区61","49"],["景区71","56"],["景区81","63"],["景区91","70"]],"CommissionAmountPercent":[["景区01","7"],["景区11","14"],["景区21","21"],["景区31","28"],["景区41","35"],["景区51","42"],["景区61","49"],["景区71","56"],["景区81","63"],["景区91","70"]],"ProfitAmountPercent":[["景区01","7"],["景区11","14"],["景区21","21"],["景区31","28"],["景区41","35"],["景区51","42"],["景区61","49"],["景区71","56"],["景区81","63"],["景区91","70"]]}
I want to
{"TicketNumPercent":[["景区01",10].....}
How Convert to decimal Type when Serials ?
Now,My Code Below:
var dataList = dt.Rows.Cast<DataRow>().Select(x => new
{
TicketNumPercent = new[] { x["Scence"],x["TicketNumPercent"] },
SalePricePercent = new[] { x["Scence"],x["SalePricePercent"] },
CostPricePercent = new[] { x["Scence"], x["CostPricePercent"] },
CommissionAmountPercent = new[] { x["Scence"],x["CommissionAmountPercent"] },
ProfitAmountPercent = new[] { x["Scence"], x["ProfitAmountPercent"] }
}).ToList();
var jsonModel = new
{
TicketNumPercent = dataList.Select(x => x.TicketNumPercent),
SalePricePercent = dataList.Select(x => x.SalePricePercent),
CostPricePercent = dataList.Select(x => x.CostPricePercent),
CommissionAmountPercent=dataList.Select(x=>x.CommissionAmountPercent),
ProfitAmountPercent=dataList.Select(x=>x.ProfitAmountPercent)
};
return jsonModel;
@alone2013: The standard json format is like {"":""}.If you want to use number format in output string,you can change your serialize tools like FastJson or use the custom serials format!And I want to know what's the serialize tool you used?
鸟语1级,见笑了!
@幻天芒:
比我强多了,向你学习哈!
The serialize too is:
James Newton-King - Json.NET
http://james.newtonking.com/json
How to solve the problem?
Thanks!
@alone2013:I guess it!You can try for example:
var dataList = dt.Rows.Cast<DataRow>().Select(x => new { TicketNumPercent = new[] { x["c"], this.GetInt(x["col1"]) }, SalePricePercent = new[] { x["c"], this.GetInt(x["col2"]) }, CostPricePercent = new[] { x["c"], this.GetInt(x["col3"]) } }).ToList(); private int GetInt(object obj) { return Convert.ToInt32(obj ?? "0"); }
@幻天芒:
Can I set data type is decimal before serialize?
How to change the code ?
@幻天芒:
Well done!
Thanks very much appreciated.
@alone2013: You're welcome!
@幻天芒:
Hi, how to highlight code in q.cnblog?
I try put the code in <div class="cnblogs_code"><pre>...some code..</pre></div>
no highlight...
@alone2013: In the text editor,you can click the reciprocal second button in the toolbar,and then you can insert code!
@幻天芒:
Ok! Thanks.
If you have time,help me solve a problem please!
Link to:
DataTable加工后添加行列,输出求和,百分比的问题请教!_博问_博客园
http://q.cnblogs.com/q/58983/
@alone2013: okay!
若我的DataTable最后一行是合计行,当转为dataList时,我要忽略最后一行,
以下代码要怎样修改呢? 对linq不了解,不知道怎样改...
var dataList = dt.Rows.Cast<DataRow>().Select(x => new { TicketNumPercent = new[] { x["c"], x["col1"] }, SalePricePercent = new[] { x["c"], x["col2"] }, CostPricePercent = new[] { x["c"], x["col3"] } }).ToList();
@alone2013:Append code:
dataList=dataList.Take(dataList.Count-1);
@幻天芒:
I try below:
dataList =dataList.Take(dataList.Count - 1).ToList();
ok!Thanks.
@alone2013: No thanks!
@幻天芒:
Your linq skill is good!
I Have a trouble..
How Do I Use Gruop By Method In Charp Code To Covert DataTable?
http://q.cnblogs.com/q/59054/
If not through ling metho.
Direct manipulation datatable,how can I do?
@alone2013: What's wrong?