如何使用File.WriteAllLines对文本进行编码?



我试图制作一个 txt 文件,然后将扩展名重命名为 .json,我已经完成了编码步骤和 WriteAllLines 步骤,但是我如何对文本进行编码?我有需要写的字符串(

这是代码

string[] lines = { "{", ""version": 1,", ""schema_version": 2,", "", 
$""id": "{textBox14.Text}",", "", $""title": "{textBox7.Text}",", 
$""title_localized": "{textBox18.Text}",", "", $""artist": "{textBox6.Text}",", 
$""artist_localized": "{textBox8.Text}",", $""artist_source": "{textBox9.Text}",", 
$"", $""illustrator": "{textBox10.Text}",", $""illustrator_source": "{textBox11.Text}",", 
$"", $""charter": "{textBox13.Text}",", $"", ""music": {", 
$""path": "{textBox4.Text}"", "}", ""music_preview": {", $""path": "{textBox5.Text}"", "}", 
""background": {", $""path": "{open3.FileName}"", "}", 
""charts": [", "{", ""type": "easy",", $""name": "{textBox15.Text}",", 
$""difficulty": {numericUpDown1.Value},", $""path": "textBox1.Text"", "},", 
"{", ""type": "hard",", $""name": "{textBox16.Text}",", $""difficulty": {numericUpDown2.Value},", 
$""path": "{textBox2.Text}"", "},", $"]", $"", "}" };
Encoding utf8WithoutBom = new UTF8Encoding(true);
File.WriteAllLines($@"C:UsersPublicDesktoplevel files{textBox14.Text}level.json", lines);

它应该是这样的: https://cytoid.io/level.json

简短回答:

更改此设置:

File.WriteAllLines($@"C:UsersPublicDesktoplevel files{textBox14.Text}level.json", lines);

对此:

File.WriteAllLines($@"C:UsersPublicDesktoplevel files{textBox14.Text}level.json", lines, utf8WithoutBom);

长答案:

你不应该像这样生成JSON;你应该使用专用的序列化程序。对于当前解决方案,如果用户输入无效字符,JSON 将立即变为无效。因此,作为解决方案,您可以使用Newtonsoft的 JSON.Net。 下面是一个示例:

类定义

public class Item
{
public int Version { get; set; }
public int SchemaVersion { get; set; }
public string Id { get; set; }
public string Title { get; set; }
public string TitleLocalized { get; set; }
public string Artist { get; set; }
public string ArtistLocalized { get; set; }
public string ArtistSource { get; set; }
public string Illustrator { get; set; }
public string IllustratorSource { get; set; }
public string Charter { get; set; }
public ItemMusic Music { get; set; }
public ItemMusicPreview MusicPreview { get; set; }
public ItemBackground Background { get; set; }
public List<ItemChart> Charts { get; set; }
}
public class ItemMusic
{
public string Path { get; set; }
}
public class ItemMusicPreview
{
public string Path { get; set; }
}
public class ItemBackground
{
public string Path { get; set; }
}
public class ItemChart
{
public string Type { get; set; }
public string Name { get; set; }
public int Difficulty { get; set; }
public string Path { get; set; }
}

对象初始化和序列化

var item = new Item
{
Version = 1,
SchemaVersion = 2,
Id = textBox14.Text,
Title = textBox7.Text,
TitleLocalized = textBox18.Text,
Artist = textBox6.Text,
ArtistLocalized = textBox8.Text,
ArtistSource = textBox9.Text,
Illustrator = textBox10.Text,
IllustratorSource = textBox11.Text,
Charter = textBox13.Text,
Music = new ItemMusic
{
Path = textBox4.Text
},
MusicPreview = new ItemMusicPreview
{
Path = textBox5.Text
},
Background = new ItemBackground
{
Path = open3.FileName
},
Charts = new List<ItemChart>
{
new ItemChart
{
Type = "easy",
Name = textBox15.Text,
Difficulty = numericUpDown1.Value,
Path = textBox1.Text
},
new ItemChart
{
Type = "hard",
Name = textBox16.Text,
Difficulty = numericUpDown2.Value,
Path = textBox2.Text
}
}
};
var settings = new JsonSerializerSettings()
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
var json = JsonConvert.SerializeObject(item, settings);
File.WriteAllText($@"C:UsersPublicDesktoplevel files{textBox14.Text}level.json", json, new UTF8Encoding(true));

当然,您也可以使用匿名类型而不是创建完整的类定义:

var item = new {
Version = 1,
SchemaVersion = 2,
Charts = new List<object>
{
new {
Type = "easy"
}
}
}

然后只是序列化它。

最新更新