• 将json字符串转换为DataTable


    字符串

     {

    "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;
                }

  • 相关阅读:
    企业生产环境不同业务linux系统分区方案
    linux 文件 s 权限
    shell中的命令与特殊符号
    Linux数组基础
    shell脚本学习(1)
    文件的压缩与打包
    Linux 磁盘管理基础命令df,du,fdisk,mke2fs
    mkpasswd的使用
    P1080 国王游戏
    P1315 观光公交
  • 原文地址:https://www.cnblogs.com/lq0418/p/9025505.html
Copyright © 2020-2023  润新知