我正在开发嵌入式应用程序。 每当厨师开始烹饪时,我都会在应用程序中存储烹饪数据(例如设定点温度,烹饪类型,食谱名称等)。
现在,我正在构建下载页面,用户可以使用上面列出的信息从该页面下载csv文件。我该怎么做,然后解压缩csv文件?
p.s我以Json格式存储数据。我将在下面复制一个示例。
haccp.json文件:
{
"alarm_message": "",
"alarm_title": "",
"alarm_type": -1,
"alarm_value": -1,
"color": "#b96265",
"date": "20 giu 2019",
"details": [
{
"chamber_rd": "0",
"chamber_sp": "180",
"core_rd": "0",
"core_sp": "--",
"phase": "1",
"steam_mode": "Conv",
"time": "17:58"
},
{
"chamber_rd": "0",
"chamber_sp": "180",
"core_rd": "0",
"core_sp": "--",
"phase": "1",
"steam_mode": "Conv",
"time": "17:58"
}
],
"end": "17:59",
"is_alarm": false,
"start": "17:58",
"type": "Manual"
}
更新: csv文件应具有以下列格式: 类型,日期,开始,结束,时间,蒸汽模式,相位,腔室RD,腔室SP,核心RD,核心SP
这也是我保存和写入Json文件的方式:
void HaccpModel::start_program(QString program_name)
{
running_program = true;
running_program_name = program_name;
int sample_step = 3000;
sample_step_timer.start(sample_step);
hi_current.type = program_name;
hi_current.date =
QDate::currentDate().toString(QStringLiteral("dd MMM yyyy"));
hi_current.start = QTime::currentTime().toString(QStringLiteral("hh:mm"));
hi_current.color = get_color();
haccp_list.emplace_back(hi_current);
}
void HaccpModel::stop_program()
{
haccp_list.back().end =
QTime::currentTime().toString(QStringLiteral("hh:mm"));
sort_haccp();
if (!running_program) {
return;
}
running_program = false;
sample_step_timer.stop();
save_haccp();
}
// I call this function from a connect, connected to the timer
void HaccpModel::add_haccp_details()
{
HaccpDetail d;
d.time = QTime::currentTime().toString(QStringLiteral("hh:mm"));
d.phase = QString::number(
MachineState::get_instance().get_current()->get_current_phase() + 1);
d.steam_mode = get_steam_mode(
MachineState::get_instance().get_current()->get_oven_mode());
d.chamber_sp = QString::number(
MachineState::get_instance().get_current()->get_oven_temp());
d.chamber_rd = QString::number(ps->get_oven_probe_gui());
d.core_sp = "--";
d.core_rd = QString::number(ps->get_probe1_gui());
haccp_list.back().details.emplace_back(d);
sort_haccp();
}
void HaccpModel::save_haccp()
{
auto haccp_file =
Path().get_haccp_path() +
QStringLiteral("haccp_%1.json")
.arg(QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));
Serializer().write(this, haccp_file);
}
void Serializer::write(ISerializable *subject, const QString &filename)
{
QFile f(filename);
if (!f.open(QIODevice::ReadWrite)) {
Logger::singleton.log(
QtWarningMsg, serialization(),
QStringLiteral("can't open %1 in write mode").arg(filename));
}
f.resize(0);
QJsonDocument doc(subject->get_json_obj());
f.write(doc.toJson());
}
谢谢
答案 0 :(得分:2)
您可以读取文件并进行如下解析:
QFile file("haccp.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
auto jsonDoc = QJsonDocument::fromJson(file.readAll()).object();
file.close();
CSV可以这样写,请注意QIODevice::Truncate
标志。
QFile csv("haccp.csv");
csv.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
QTextStream out(&csv);
// CSV Header
out << "Type,Date,Start,End,Time,Steam Mode,Phase,Chamber RD,Chamber SP,Core RD,Core SP";
endl(out);
for (auto d : json.value("details").toArray()) {
auto detail = d.toObject();
out << json.value("type").toString() << ',';
out << json.value("date").toString() << ',';
out << json.value("start").toString() << ',';
out << json.value("end").toString() << ',';
out << detail.value("time").toString() << ',';
out << detail.value("steam_mode").toString() << ',';
out << detail.value("phase").toString() << ',';
out << detail.value("chamber_rd").toString() << ',';
out << detail.value("chamber_sp").toString() << ',';
out << detail.value("core_rd").toString() << ',';
out << detail.value("core_sp").toString() << ',';
endl(out);
}
csv.close();
您的示例json将产生以下CSV:
Type,Date,Start,End,Time,Steam Mode,Phase,Chamber RD,Chamber SP,Core RD,Core SP
Manual,20 giu 2019,17:58,17:59,17:58,Conv,1,0,180,0,--,
Manual,20 giu 2019,17:58,17:59,17:58,Conv,1,0,180,0,--,