根据文件名自动生成JSON脚本文件



我想使用JSON文件作为模板来创建新的JSON文件,脚本中有几个单词会根据文件名而变化。

例如,假设我有一个名为word1.JSON的JSON文件,其脚本如下所示:

filename:word1.json

脚本:

{
"name": "word1",
"annotates": "word1.wav",
"sampleRate": 44100
}

我想为我输入的每个文件名自动生成一个新的JSON脚本,例如:

filename:word2.json

脚本:

{
"name": "word2",
"annotates": "word2.wav",
"sampleRate": 44100
}

filename:newword.json

脚本:

{
"name": "newword",
"annotates": "newword.wav",
"sampleRate": 44100
}

等等

有什么软件可以让我这么做吗?我目前正在用TextEdit编辑JSON文件——你可能知道,我不是程序员,但我可以使用R,并可以在那里做一些编程。

编辑:我在MacOS 上

非常感谢。

我可能会在这里受到一些讨厌,但由于你是新手,你不是程序员,这只是一个简单的请求,我说"每个人都有一个"。

这是一个C#程序,可以在任何兼容的IDE中运行。如果没有安装,可以使用LinqPad运行。这是一个快速而简单的安装。打开LinqPad并作为C#程序运行。按下播放按钮启动程序。

您将需要添加NuGet";Newtonsoft.Json";。

此程序将在您的硬盘驱动器C:\jsonfiles中创建一个新目录。它会反复要求您输入文件名,直到您输入"退出",此时程序将退出。根据需要重新运行。

void Main()
{
Console.WriteLine("JSON Creator");
Console.WriteLine("For each entered file name a JSON file will be created. The files will be created at C:/jsonfiles. If the file already exists it will be overwritten.");
Console.WriteLine("Enter 'exit' to end the program");
string jsonDirectory = @"C:jsonfiles";
CreateBaseDirectory(jsonDirectory);
string jsonFileName = GetUserInput();
while (QuitProgram(jsonFileName) == false)
{
string fullJsonPath = System.IO.Path.Combine(jsonDirectory, jsonFileName + ".json");
try
{
DeleteExistingFile(fullJsonPath);
}
catch (IOException ioException)
{
Console.WriteLine("Unable to delete file. The file may be in use. Please close the file or manually delete the file before continuing on.");
jsonFileName = GetUserInput();
continue;
}
try
{
SaveToFile(jsonFileName, fullJsonPath);
}
catch (Exception ex)
{
Console.WriteLine("There was an issue creating the file. Verify the file does not already exist, the file name is valid, the file is not open, and you have privileges to write to the file system");
jsonFileName = GetUserInput();
continue;
}
jsonFileName = GetUserInput();
}
}
private void CreateBaseDirectory(string jsonDirectory)
{
if (System.IO.Directory.Exists(jsonDirectory) == false)
{
System.IO.Directory.CreateDirectory(jsonDirectory);
}
}
private bool InputIsValid(string input)
{
return string.IsNullOrWhiteSpace(input) == false;
}
private bool QuitProgram(string input)
{
string modifiedInput = input?.Trim()?.ToLower();
return modifiedInput == "quit" || modifiedInput == "q" || modifiedInput == "exit";
}
private string GetUserInput()
{
Console.WriteLine("Please enter in the JSON file name");
string input = Console.ReadLine();
while (InputIsValid(input) == false)
{
Console.WriteLine("Please enter valid JSON file name. An empty or space-only file name is not valid.");
}
return input.Trim().Replace(".json", "");
}
private void DeleteExistingFile(string fullJsonPath)
{
if (System.IO.File.Exists(fullJsonPath))
{
File.Delete(fullJsonPath);
}
}
private void SaveToFile(string jsonFileName, string fullJsonPath)
{
StringBuilder builder = new StringBuilder();
JsonTemplate template = new JsonTemplate();
template.name = jsonFileName;
template.annotates = jsonFileName + ".wav";
System.IO.File.WriteAllText(fullJsonPath, Newtonsoft.Json.JsonConvert.SerializeObject(template));
Console.WriteLine(jsonFileName + " has been created");
}
private class JsonTemplate
{
public string name { get; set; }
public string annotates { get; set; }
public int sampleRate { get; set; } = 44100;
}

编辑

我刚刚注意到你在使用MacOS。我没有访问权限,也没有太多关于通过MacOS编程的知识。至少,您需要更新保存目录的位置。

最新更新