locked
Write data to json type file using c# RRS feed

  • Question

  • User-257070954 posted

    Hi All

               How we write data to json type file using c#

    [
    {"title":"","subtitle":"","ranges":[50,70,100],"measures":[50,70],"markers":[0]}
    ]

    i want to write above data to abc.json file using c#. How?

    Tuesday, March 27, 2018 10:48 AM

All replies

  • User516094431 posted

     How we write data to json type file using c#

    [
    {"title":"","subtitle":"","ranges":[50,70,100],"measures":[50,70],"markers":[0]}
    ]

    As per your need!!!

    public class jsonData
        {
            public string title { get; set; }
            public string subtitle { get; set; }
            public int[] ranges { get; set; }
            public int[] measures { get; set; }
            public int[] markers { get; set; }
        }
    
    List<jsonData> _data = new List<jsonData>();
    _data.Add(new jsonData()
    {
        title = "",
        subtitle = "",
        ranges = [20,30,40],
        measures = [50,64],
        markers = []
    });
    
    string json = JsonConvert.SerializeObject(_data.ToArray());
    
    //write string to file
    System.IO.File.WriteAllText(@"D:\jsonFile.json", json);

    Tuesday, March 27, 2018 11:16 AM
  • User-1838255255 posted

    Hi binustrat,

    According to your description and code, i make a sample through your needs, please check the detailed code:

    public class Movie
            {
                public string title { get; set; }
                public string subtitle { get; set; }
                public int[] ranges { get; set; }
                public string measures { get; set; }
                public string markers { get; set; }
            }
    
    protected void Button4_Click(object sender, EventArgs e)
            {
                var jsonInput = "{ 'title':'','subtitle':'','ranges':[50,70,100],'measures':'70','markers':'0'}";
                Movie movie = Newtonsoft.Json.JsonConvert.DeserializeObject<Movie>(jsonInput);
    
                File.WriteAllText(@"D:\movie.json", JsonConvert.SerializeObject(movie));
    
                var path = Server.MapPath(@"~/movie.json");
    
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
                {
                    file.WriteLine(movie);
                }
            }

    Best Regards,

    Eric Du 

    Wednesday, March 28, 2018 7:06 AM
  • User-1430215351 posted

    Hi Eric,

    why do we DeSerializing and then Serializing JsonInput?

    if you are writing in a file, string is already with you, you can directly write

    Commented a lines that are not require.

    Protected void Button1_Click(object sender, EventArgs e)

    {

    var jsonInput = "{ 'title':'','subtitle':'','ranges':[50,70,100],'measures':'70','markers':'0'}";

    // Movie movie = Newtonsoft.Json.JsonConvert.DeserializeObject<movie>(jsonInput); </movie>

    <movie>// File.WriteAllText(@"C:\Users\Leena\source\WebSites\WebSite5\movie.json", JsonConvert.SerializeObject(movie)); </movie>

    <movie>File.WriteAllText(@"C:\Users\Leena\source\WebSites\WebSite5\movie.json", jsonInput); var path = Server.MapPath(@"~/movie.json"); </movie>

    <movie>// using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true)) // { // file.WriteLine(movie); </movie>

    <movie>// } </movie>

    <movie>Response.Write("done");</movie>

    <movie> }</movie>

    Friday, January 29, 2021 4:38 PM