Add json file operation

This commit is contained in:
daleclack 2022-08-08 19:01:31 +08:00
parent 4784308c67
commit 17281e8023
1 changed files with 29 additions and 1 deletions

View File

@ -126,5 +126,33 @@ void MediaPlayer::btnload_clicked()
void MediaPlayer::btnsave_clicked()
{
// Initalize Vectors
std::vector<std::string> name_tmp, path_tmp;
for (auto row : store->children())
{
name_tmp.push_back(row[n_columns.file_name]);
path_tmp.push_back(row[n_columns.file_path]);
}
// Save data to json file
std::fstream outfile;
outfile.open("playlist.json", std::ios::out);
if (outfile.is_open())
{
// Load json data
json data = json::parse(R"(
{
"name":[""],
"path":[""]
}
)");
data["name"] = name_tmp;
data["path"] = path_tmp;
// Save to file
outfile << data;
}else{
std::cout << "Failed to save file!" << std::endl;
}
outfile.close();
}