How to write a json file in c #?

Hey. Do I need to write the following data to a text file using Json format in C #? ASSEMBLIES IMPORTANT FOR A VALID JSON FORMAT

[ { "Id": 1, "SSN": 123, "Message": "whatever" }, { "Id": 2, "SSN": 125, "Message": "whatever" } ] 

and here is my model class

 public class data { public int Id { get; set; } public int SSN { get; set; } public string Message {get; set;} } 
+125
json c # asp.net-web-api
Jun 04 '13 at 15:23
source share
5 answers

I would recommend Json.Net , see example below:

 List<data> _data = new List<data>(); _data.Add(new data() { Id = 1, SSN = 2, Message = "A Message" }); string json = JsonConvert.SerializeObject(_data.ToArray()); //write string to file System.IO.File.WriteAllText(@"D:\path.txt", json); 

Or a slightly more efficient version of the above code (doesn't use a string as a buffer):

 //open file stream using (StreamWriter file = File.CreateText(@"D:\path.txt")) { JsonSerializer serializer = new JsonSerializer(); //serialize object directly into file stream serializer.Serialize(file, _data); } 

Documentation: Serialize JSON to File




Why? It compares a comparison of conventional serializers as well as test cases โ€ .

The following is a graph of performance taken from a related article:

enter image description here

This separate post indicates that:

Json.NET has always been efficient in terms of memory, streaming reading and writing large documents, rather than loading them completely into memory, but I was able to find a couple of key places where you could reduce the allocation of objects ...... (now) Json.Net (6.0) allocates 8 times less memory than JavaScriptSerializer

โ€ก.

โ€  The tests look like Json.Net 5, the current version (when recording) is 10. What version of standard .Net serializers is not mentioned

โ€ก These tests are obviously from developers who support this library. I have not confirmed my claims . If in doubt, check them out yourself.

+233
Jun 04 '13 at 15:25
source share

The example in Liam's answer saves the file as a line on one line. I prefer to add formatting. Someone in the future may want to change some value manually in the file. If you add formatting, it will be easier to do.

The following adds a basic JSON indent:

  string json = JsonConvert.SerializeObject(_data.ToArray(), Formatting.Indented); 
+53
Nov 07 '13 at 14:50
source share

There is built-in functionality for this using the JavaScriptSerializer Class :

 var json = JavaScriptSerializer.Serialize(data); 
+7
Jun 04 '13 at 15:26
source share
 var responseData = //Fetch Data string jsonData = JsonConvert.SerializeObject(responseData, Formatting.None); System.IO.File.WriteAllText(Server.MapPath("~/JsonData/jsondata.txt"), jsonData); 
+4
Nov 05 '16 at 22:33
source share

This is a simpler implicit way to write a json object in C #

 var returnData = new[] { new{StudentId = 101, StudentName = "Joseph Leung"}, new{StudentId = 102, StudentName = "Pravin Jha"} }; 
-5
May 09 '16 at 20:54
source share



All Articles