好日子,格式的StreamWriter格式無效寫/追加的Json
我怎麼能保存/追加中的JSON數據與StreamWriter的文件中的一個有效的JSON格式?有沒有一種方法來格式化StreamWriter來以有效的JSON格式附加/寫入文件?
像:
`[
{ "data1": "data1" },
{ "appended data2": "appended data2" },
{ "appended data3": "appended data3" },
]`
我使用NewtonJson從對象序列化JSON,然後用StreamWriter的保存。
WritableData an_data = new WritableData
{
Titel = tbTitel.Text,
Type = tbType.Text,
Episode = tbEps.Text,
Score = tbScore.Text,
Id = tbID.Text,
TitleEng = tbTitelEng.Text,
Status = tbStatus.Text,
StartDate = tbDateStart.Text,
EndDate = tbDateEnd.Text,
Image = pbImage.ImageLocation
};
string path = SavePath + "\\AnList";
string json = JsonConvert.SerializeObject(an_data, Formatting.Indented);
TextWriter tw = new StreamWriter(path + listFile, true);
tw.WriteLine(json);
tw.Close();
而且它被保存如下:
{ "Titel": "Test1", "Type": "Movie", "Episode": "1", "Score": "6.92", "Id": "894", "TitleEng": "Test1", "Status": "Finished Airing", "StartDate": "1989-07-15", "EndDate": "1989-07-15", "Image": "https://myanimelist.cdn-dena.com/images/anime/5/10193.jpg" }{ "Titel": "Test2", "Type": "TV", "Episode": "153", "Score": "8.16", "Id": "223", "TitleEng": "Test2", "Status": "Finished Airing", "StartDate": "1986-02-26", "EndDate": "1989-04-12", "Image": "https://myanimelist.cdn-dena.com/images/anime/9/21055.jpg" }
我無法找到一個方法來正確格式化。
謝謝您的時間。
你爲什麼要這麼做?使用JObject並添加一個屬性,你想要寫整個文件 –
@Jamiec我編輯了代碼,其中包含'an_data'的數據 – idkWhatToDo
@FilipCordas我認爲這將是保存對象爲JSON的最佳方式。我是新來的C#和編程,所以如果有更好的方式,我會使用它 – idkWhatToDo