该随笔仅用于记录个人在项目中使用的方法,用于学习和回顾
QString json_file = QFileDialog::getOpenFileName(this, tr("json File"), "C:\\", "JSON(*.json)"); //获取文件路径 QFile jsondoc(json_file); //创建文件对象 jsondoc.open(QIODevice::ReadOnly); //只读模式打开 QByteArray json_data = jsondoc.readAll(); //以QByteArray类型的方式获取json全部内容,内容读取到json_data内 jsondoc.close(); //关闭文件
QString write_json_path = QFileDialog::getSaveFileName(this,"json_file","C:\\",tr("JSON(*.json)")); //获取要写入的json文件路径 QFile write_json(write_json_path); //创建文件对象 if(write_json_path.isEmpty()) { QMessageBox::warning(this, tr("tip"), tr("write_json file creating failure!")); } else { if(!write_json.open(QIODevice::WriteOnly)) { QMessageBox::warning(this, tr("tip"), tr("write_json saving failure!")); } write_json.write(json(json_document).toUtf8().data()); //将str_struct以文本形式写入文件并关闭文件 write_json.close(); } }