首页 > Web开发 > 详细

将json字符串转换为DataTable

时间:2018-05-11 18:04:40      阅读:181      评论:0      收藏:0      [点我收藏+]

字符串

 {

"Answer": [{
        "PatientId": "xx",
        "Question": "158",
        "AnswerContent": "3"
    }, {
        "PatientId": "aa",
        "Question": "159",
        "AnswerContent": "2"
    }]

}

 

 

 public DataTable JsonTdb(string strJson)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
             try
                {
                    
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();    //引用System.Web.Extensions
                    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                    ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(strJson);
                    if (arrayList.Count > 0)
                    {
                        foreach (Dictionary<string, object> dictionary in arrayList)
                        {
                            if (dictionary.Keys.Count<string>() == 0)
                            {
                                result = dataTable;
                                // return result;
                            }
                            if (dataTable.Columns.Count == 0)
                            {
                                foreach (string current in dictionary.Keys)
                                {
                                    dataTable.Columns.Add(current, dictionary[current].GetType());
                                }
                            }
                            DataRow dataRow = dataTable.NewRow();
                            foreach (string current in dictionary.Keys)
                            {
                                dataRow[current] = dictionary[current];
                            }

                            dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                        }
                    }
                }
                catch
                {
                }
              return   dataTable;
                // return result;
            }

将json字符串转换为DataTable

原文:https://www.cnblogs.com/lq0418/p/9025505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!