如何用C#一个JSON文件? [英] How to write a Json file in C#?
本文介绍了如何用C#一个JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我需要把下面的数据写入到使用JSON格式在C#中的文本文件?括号内是有效的重要JSON格式
[
{
标识:1,
SSN:123,
消息:无所谓 },
{
ID:2,
SSN:125,
消息:无所谓
}
]
和这里是我的模型类
公共类数据
{
公众诠释标识{搞定;组; }
公众诠释SSN {搞定;组; }
公共字符串消息{搞定;组;}}
解决方案
我会建议 Json.Net
列表<数据> _data =新的List<数据>();
_data.Add(新数据()
{
ID = 1,
SSN = 2,
消息=消息。
});
串JSON = JsonConvert.SerializeObject(_data.ToArray());//写入字符串到文件
System.IO.File.WriteAllText(@D:\\ path.txt,JSON);
文件:<一href=\"http://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm\">Serialize JSON到一个文件
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
[
{
"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;}
}
解决方案
I would recommend Json.Net
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);
Documentation: Serialize JSON to a file
这篇关于如何用C#一个JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文