Hi I need to write the following data into a text file using Json format in C#? BRACKETS are important to be valid JSON FORMAT
你好,我需要用c#中的Json格式将下面的数据写入一个文本文件中?括号对于有效的JSON格式很重要
[
{
"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;}
}
4 个解决方案
#1
162
I would recommend Json.Net, see example below:
我建议Json。净,见下面的例子:
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 the 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 a file
文档:将JSON序列化为文件
Why? Here's a feature comparision between common serialisers as well as benchmark tests .
为什么?这是一个功能比较常见serialisers之间以及基准测试。
Below is a graph of performance taken from the linked article:
以下是链接文章中的绩效图表:
This seperate post, states that:
这篇文章分别指出:
Json.NET has always been memory efficient, streaming the reading and writing large documents rather than loading them entirely into memory, but I was able to find a couple of key places where object allocations could be reduced...... (now) Json.Net (6.0) allocates 8 times less memory than JavaScriptSerializer
Json。NET一直以来都是内存高效的,它将读取和写入大型文档流到内存中,而不是将它们全部加载到内存中,但是我能够找到几个关键的位置,以便减少对象的分配…(现在)Json。Net(6.0)分配的内存比JavaScriptSerializer少8倍
.
。
Benchmarks appear to be Json.Net 5, the current version (on writing) is 10. What version of standard .Net serialisers used is not mentioned
基准似乎Json。Net 5,当前版本(写时)是10。没有提到使用的标准。net序列化器的版本
These tests are obviously from the developers who maintian the library. I have not verified their claims. If in doubt test them yourself.
这些测试显然是来自开发人员维护图书馆。我还没有证实他们的说法。如果有疑问,自己去测试一下。
#2
39
The example in Liam's answer saves the file as string in a single 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's easier to do so.
在Liam的回答中,这个例子将文件作为字符串保存在一行中。我更喜欢添加格式。将来可能有人想要在文件中手动更改一些值。如果您添加了格式,那么这样做更容易。
The following adds basic JSON indentation:
下面添加了基本的JSON缩进:
string json = JsonConvert.SerializeObject(_data.ToArray(), Formatting.Indented);
#3
7
There is built in functionality for this using the JavaScriptSerializer Class:
使用JavaScriptSerializer类为它构建了一些功能:
var json = JavaScriptSerializer.Serialize(data);
#4
2
var responseData = //Fetch Data
string jsonData = JsonConvert.SerializeObject(responseData, Formatting.None);
System.IO.File.WriteAllText(Server.MapPath("~/JsonData/jsondata.txt"), jsonData);
#1
162
I would recommend Json.Net, see example below:
我建议Json。净,见下面的例子:
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 the 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 a file
文档:将JSON序列化为文件
Why? Here's a feature comparision between common serialisers as well as benchmark tests .
为什么?这是一个功能比较常见serialisers之间以及基准测试。
Below is a graph of performance taken from the linked article:
以下是链接文章中的绩效图表:
This seperate post, states that:
这篇文章分别指出:
Json.NET has always been memory efficient, streaming the reading and writing large documents rather than loading them entirely into memory, but I was able to find a couple of key places where object allocations could be reduced...... (now) Json.Net (6.0) allocates 8 times less memory than JavaScriptSerializer
Json。NET一直以来都是内存高效的,它将读取和写入大型文档流到内存中,而不是将它们全部加载到内存中,但是我能够找到几个关键的位置,以便减少对象的分配…(现在)Json。Net(6.0)分配的内存比JavaScriptSerializer少8倍
.
。
Benchmarks appear to be Json.Net 5, the current version (on writing) is 10. What version of standard .Net serialisers used is not mentioned
基准似乎Json。Net 5,当前版本(写时)是10。没有提到使用的标准。net序列化器的版本
These tests are obviously from the developers who maintian the library. I have not verified their claims. If in doubt test them yourself.
这些测试显然是来自开发人员维护图书馆。我还没有证实他们的说法。如果有疑问,自己去测试一下。
#2
39
The example in Liam's answer saves the file as string in a single 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's easier to do so.
在Liam的回答中,这个例子将文件作为字符串保存在一行中。我更喜欢添加格式。将来可能有人想要在文件中手动更改一些值。如果您添加了格式,那么这样做更容易。
The following adds basic JSON indentation:
下面添加了基本的JSON缩进:
string json = JsonConvert.SerializeObject(_data.ToArray(), Formatting.Indented);
#3
7
There is built in functionality for this using the JavaScriptSerializer Class:
使用JavaScriptSerializer类为它构建了一些功能:
var json = JavaScriptSerializer.Serialize(data);
#4
2
var responseData = //Fetch Data
string jsonData = JsonConvert.SerializeObject(responseData, Formatting.None);
System.IO.File.WriteAllText(Server.MapPath("~/JsonData/jsondata.txt"), jsonData);