0

Initially the error this code was receiving indicated a possible file size limitation with the JsonScriptSerializer. My question is this: is there a limitation on the content of the csv? For example the csv I'm trying to convert to Json contains 23 columns and 20000 rows. If I narrow the columns to 7 and the rows to 1000 the conversion runs and converts to Json.

Here's the original code.
try {

           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);
        }
Paul
  • 79
  • 1
  • 6
  • 1
    For the setting in the web.config - it appears commented out? Did you restart the webapp after changing the setting, clear browser cache etc to make sure a completely fresh request is being made? Also confirm you have the under the node. There are some nuances for MVC - here is a good post on the topic: http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config – Murray Foxcroft Mar 15 '16 at 11:12
  • maybe you can find your solution in [this answer](http://stackoverflow.com/a/1151993/124187)? – Sam Bauwens Mar 15 '16 at 11:12
  • There are csv libraries which can deserialise csvs into objects, then you can use one line in newtonsoft json convert to serialise the object into a string (serialise the aggregate root) which would improve your code significantly – Callum Linington Mar 15 '16 at 11:15
  • That web.config setting does not apply to your scenario. See [duplicate](http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config/7207539#7207539). – CodeCaster Mar 15 '16 at 11:16
  • Commented out the web.config settings based on this post: http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config - this is not a web service. (see duplicate comments above. – Paul Mar 18 '16 at 10:48
  • Callum, can you point me to any examples of libraries that convert csv to json? – Paul Mar 18 '16 at 10:49

0 Answers0