2

I want to save data in local json file in this format:

[
{"Name":"sdafdsf","Password":"dsfads","FirstName":"fsdf","LastName":"dsfdafas"},
{"Name":"sddafdsf","Password":"dsfadds","FirstName":"fdsdf","LastName":"dsfdafasdfs"} 
]

I am using this in the controller to serialize into json format:

 public ActionResult Index(demo obj)
        {

            String json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            string path = Server.MapPath("~/app/");
            //// Write that JSON to txt file,  
            //var read = System.IO.File.ReadAllText(path + "output.json");
            System.IO.File.WriteAllText(path + "output.json",  json);
            return View();
        }

This is my model:

public class demo
    {

        public string Name { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

But instead of proper JSON format I am getting this in my output.json file:

{"Name":"sdafdsf","Password":"dsfads","FirstName":"fsdf","LastName":"dsfdafas"}{"Name":"adfsdsfsafdsafasdfsdfsadf","Password":"dfsaasdsa","FirstName":"safd","LastName":"dfsafads"}

How can I save data in the proper format?

4
  • The format you show would require serializing an array of demo, so something like List<demo> would be easy. Commented Dec 27, 2017 at 11:52
  • Your first listing is an ARRAY. And your code manipulates ONE object. Commented Dec 27, 2017 at 11:52
  • If I use List<demo> then data will store in that format but if I add another data then It will In this format ::: [{"Name":"sdafdsf","Password":"dsfads","FirstName":"fsdf","LastName":"dsfdafas"}][{"Name":"adfsdsfsafdsafasdfsdfsadf","Password":"dfsaasdsa","FirstName":"safd","LastName":"dfsafads"}] Commented Dec 27, 2017 at 11:53
  • It sounds like you need to change your code so that your ActionResult is called only once when you have a completed array of demo to pass to it. Commented Dec 27, 2017 at 11:57

2 Answers 2

5

This is the proper format, if you mean you need it like an array then add the object to an array and after that serialize it:

 public ActionResult Index(demo obj)
        {
    var array = new [] {obj};
            String json = Newtonsoft.Json.JsonConvert.SerializeObject(array);
            string path = Server.MapPath("~/app/");
            //// Write that JSON to txt file,  
            //var read = System.IO.File.ReadAllText(path + "output.json");
            System.IO.File.WriteAllText(path + "output.json",  json);
            return View();
        }  

Or:

var list = new List<demo>();
list.Add(obj);
String json = Newtonsoft.Json.JsonConvert.SerializeObject(list);

if you want to keep data always in an array then you always need to:

  1. read data from the json file.
  2. deserialize as a list of `List'.
  3. add the new item to this list.
  4. serialize it again and save it over that file replacing all the old text with the new one.

like this:

 public ActionResult Index(demo obj)
        {
          string path = Server.MapPath("~/app/");
        var read = System.IO.File.ReadAllText(path + "output.json");
        List<demo> lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<demo>>(read);
        if (lst == null)
        {
            List<demo> _data = new List<demo>();
            _data.Add(obj);
           String json = Newtonsoft.Json.JsonConvert.SerializeObject(_data.ToArray());
            System.IO.File.WriteAllText(path + "output.json", json);
        }
        else
        {
            lst.Add(obj);
            String json = Newtonsoft.Json.JsonConvert.SerializeObject(lst);
            System.IO.File.WriteAllText(path + "output.json", json);
        }
        return View();
        }  

Of course you can write cleaner code by separate some pieces.

Sign up to request clarification or add additional context in comments.

3 Comments

IN output.json format is like ::: [{"Name":"asdfdsfsadf","Password":"adfasdf","FirstName":"fasdf","LastName":"asdf"}][{"Name":"asdfdsfsadfsaf","Password":"adfasdffsadds","FirstName":"fasdffdsfds","LastName":"asdffsdfsdf"}]
getting error at lst.Add(obj) :: ERROR == Object reference not set to an instance of an object.
Thanks I accept your answer , and please up my question.
0

Create a List of objects first and then try to Serialize the list. I'm sure you will get the desired output. Seems like you used AppendAllText method by Serializing single object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.