首页 新闻 会员 周边 捐助

DataTableToObject[][]_To_Json_问题请教

0
悬赏园豆:10 [已解决问题] 解决于 2014-01-16 14:11

//我想要的最终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 { getset; }
 
        [JsonProperty("SalePricePercent")]
        public object[][] SalePricePercent { getset; }
 
        [JsonProperty("CostPricePercent")]
        public object[][] CostPricePercent { getset; }
 
        [JsonProperty("CommissionAmountPercent")]
        public object[][] CommissionAmountPercent { getset; }
 
        [JsonProperty("ProfitAmountPercent")]
        public object[][] ProfitAmountPercent { getset; }
   	    
		
		//在抽取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;
		}

如果上面的转换方式显得太笨拙了,还有其它更好的方法么?请指教!

alone2013的主页 alone2013 | 初学一级 | 园豆:46
提问于:2014-01-15 11:45
< >
分享
最佳答案
0
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?

再优化下,可以提取为公共方法!
收获园豆:10
幻天芒 | 高人七级 |园豆:37207 | 2014-01-15 12:15

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 | 园豆:46 (初学一级) | 2014-01-15 12:55

@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级,见笑了!

幻天芒 | 园豆:37207 (高人七级) | 2014-01-15 13:11

@幻天芒: 

比我强多了,向你学习哈!

The serialize too is:

James Newton-King - Json.NET
http://james.newtonking.com/json

How to solve the problem?

Thanks!

alone2013 | 园豆:46 (初学一级) | 2014-01-15 13:45

@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");
        }
幻天芒 | 园豆:37207 (高人七级) | 2014-01-15 14:10

@幻天芒: 

Can  I set data type is decimal before  serialize?

How to change the code ?

alone2013 | 园豆:46 (初学一级) | 2014-01-15 14:13

@幻天芒: 

Well done!

Thanks very much appreciated.

alone2013 | 园豆:46 (初学一级) | 2014-01-15 14:18

@alone2013: You're welcome!

幻天芒 | 园豆:37207 (高人七级) | 2014-01-15 14:29

@幻天芒: 

 

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 | 园豆:46 (初学一级) | 2014-01-15 14:39

@alone2013: In the text editor,you can click the reciprocal second button in the toolbar,and then you can insert code!

幻天芒 | 园豆:37207 (高人七级) | 2014-01-15 14:54

@幻天芒: 

Ok! Thanks.

If you have time,help me solve a problem please!

Link to:

DataTable加工后添加行列,输出求和,百分比的问题请教!_博问_博客园
http://q.cnblogs.com/q/58983/

alone2013 | 园豆:46 (初学一级) | 2014-01-15 15:22

@alone2013: okay!

幻天芒 | 园豆:37207 (高人七级) | 2014-01-15 17:23

若我的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 | 园豆:46 (初学一级) | 2014-01-16 10:24

@alone2013:Append code:

dataList=dataList.Take(dataList.Count-1);

 

幻天芒 | 园豆:37207 (高人七级) | 2014-01-16 11:44

@幻天芒: 

 

 

I try below:

       dataList =dataList.Take(dataList.Count - 1).ToList();

ok!Thanks.

alone2013 | 园豆:46 (初学一级) | 2014-01-16 11:51

@alone2013: No thanks!

幻天芒 | 园豆:37207 (高人七级) | 2014-01-16 13:14

@幻天芒: 

 

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/

alone2013 | 园豆:46 (初学一级) | 2014-01-17 18:00

If not through ling metho.

Direct manipulation datatable,how can I do?

alone2013 | 园豆:46 (初学一级) | 2014-01-23 13:13

@alone2013: What's wrong?

幻天芒 | 园豆:37207 (高人七级) | 2014-01-23 14:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册