写入现有JSON文件不会覆盖旧内容

时间:2015-03-16 10:44:50

标签: json qt

我将使用c ++ / Qt覆盖现有的JSON文件。我搜索了很多,并有兴趣关注this answer,它出现在我的问题的答案中。

我能够使用它写入JSON文件。但问题是现有内容仍在文件中。

这是我的代码。

void ServerRecordCreator::writeToLocal(Record *record) const
{
    QFile json_records_file(QDir::currentPath() + "/WrittenRecords.json");
    if (json_records_file.open(QIODevice::ReadWrite))
    {
        QJsonParseError error;
        QJsonDocument json_records_document(QJsonDocument::fromJson(json_records_file.readAll(),&error));
        if(!json_records_document.isNull()){
            QJsonObject json_root_object;
            d->writeRecord(record,json_root_object,json_records_document);
            QJsonDocument new_document(json_root_object);
            json_records_file.write(new_document.toJson());
        }else
        {
            qDebug() << error.errorString();
        }
    }
    else
    {
        qDebug("Cannot found or open the WrittenRecords.json file %s:%d", __FILE__ , __LINE__);
        return;
    }
}
 void writeRecord(Record *record, QJsonObject &json_root_object,QJsonDocument &json_document) const
    {
        bool record_found = false;
        QJsonObject json_record_object;
        record->write(json_record_object);

        if(!json_document.isNull()){
            QJsonObject::iterator i;
            for(i= json_document.object().begin();i !=json_document.object().end();i++)
            {
                QVariant key = i.key();
                if(record->type() == record->stringToRecordType(key.toString()))
                {
                    QJsonValue records =json_document.object().value(i.key());
                    QJsonArray records_array = records.toArray();
                    records_array.push_back(json_record_object);
                    json_root_object[record->recordTypeToString(record->type())] = records_array;
                    record_found = true;
                }
            }
        }

        if(!record_found)
        {
            json_root_object[record->recordTypeToString(record->type())] = json_record_object;
        }
    }

写入之前的WrittenRecords.json文件。

{
    "ATHLETE_RECORD": [
        {
            "club": "BCsC",
            "date_of_birth": "Mon Jan 5 2015",
            "first_name": "Lakshan",
            "id_gender": 2,
            "last_name": "Perera"
        }
    ]
}

之后

{
    "ATHLETE_RECORD": [
        {
            "club": "BCsC",
            "date_of_birth": "Mon Jan 5 2015",
            "first_name": "Lakshan",
            "id_gender": 2,
            "last_name": "Perera"
        }
    ]
}

{
    "ATHLETE_RECORD": [
        {
            "club": "BCsC",
            "date_of_birth": "Mon Jan 5 2015",
            "first_name": "Lakshan",
            "id_gender": 2,
            "last_name": "Perera"
        },
        {
            "club": "NdCC",
            "date_of_birth": "Mon Jan 5 2015",
            "first_name": "Lakshan",
            "id_gender": 2,
            "last_name": "Perera"
        }
    ]
}

感谢阅读。

0 个答案:

没有答案