所以我当前正在将json数据打印到文件中。
private void btnUnban_Click(object sender, RoutedEventArgs e)
{
BannedUsers user = lvBannedUsers.SelectedItem as BannedUsers;
bannedUsernames.Remove((BannedUsers)user);
lvBannedUsers.Items.Refresh();
using (StreamWriter sw = new StreamWriter("banned-players.json"))
{
sw.Write("[" + Environment.NewLine);
foreach (BannedUsers userdata in bannedUsernames)
{
string serializedData = JsonConvert.SerializeObject(userdata);
sw.Write(serializedData + "," + Environment.NewLine);
}
sw.Write("]");
}
}
问题是当它打印出来时,它看起来像这样。
[
{"uuid":null,"name":"steffe","created":null,"source":null,"expires":null,"reason":null}
]
应该看起来像这样。
[
{
"uuid": "a7c1987f-022c-4310-bd32-21614e7e37b8",
"name": "Stefan",
"created": "2017-11-09 21:54:40 +0100",
"source": "Server",
"expires": "forever",
"reason": "Banned by an operator."
}
]
如何正确缩进json数据?因为当外部应用程序读取数据时会抛出错误,因为由于缩进问题而无法正确读取它。