C#将CSV转换为Json - 序列化结果总是" System.Web.Script.Serialization.JavaScriptSerializer"?

时间:2016-03-15 11:04:43

标签: c# json csv

最初,此代码接收的错误表明JsonScriptSerializer可能存在文件大小限制。我的问题是:csv的内容是否有限制?例如,尝试转换为Json的csv包含23列和20000行。如果我将列缩小到7并将行缩小到1000,则转换运行并转换为Json。

此处the original code     尝试                 {

           if (FileUpload1.PostedFile.FileName != string.Empty)
            {
                string[] FileExt = FileUpload1.FileName.Split('.');
                string FileEx = FileExt[FileExt.Length - 1];
                if (FileEx.ToLower() == "csv")
                {
                    string SourcePath = Server.MapPath("Resources//" + FileUpload1.FileName);
                    FileUpload1.SaveAs(SourcePath);
                    string Destpath = (Server.MapPath("Resources//" + FileExt[0] + ".json"));

                    StreamWriter sw = new StreamWriter(Destpath);
                    var csv = new List<string[]>();
                    var lines = System.IO.File.ReadAllLines(SourcePath);
                    foreach (string line in lines)
                        csv.Add(line.Split(','));
                    string json = new
                        System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
                    sw.Write(json);
                    sw.Close();
                    TextBox1.Text = Destpath;
                    MessageBox.Show("File is converted to json.");
                }
                else
                {
                    MessageBox.Show("Invalid File");
                }

            }
            else
            {
                MessageBox.Show("File Not Found.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

0 个答案:

没有答案