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

时间:2018-08-17 04:05:36

标签: c# windows

我试图制作一个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:\Users\Public\Desktop\level files\{textBox14.Text}\level.json", lines);

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

1 个答案:

答案 0 :(得分:1)

简短答案:

更改此:

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

对此:

File.WriteAllLines($@"C:\Users\Public\Desktop\level 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:\Users\Public\Desktop\level files\{textBox14.Text}\level.json", json, new UTF8Encoding(true));

您当然也可以使用匿名类型代替创建完整的类定义:

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

然后将其序列化。