jsonexe::jsonexe() { path = QDir::currentPath() + "/../user.json"; qDebug() << path; file.setFileName(path); } jsonexe::~jsonexe() { } QByteArray jsonexe::getJson(QStringList sL) { QJsonObject userobj,user; if(file.open(QIODevice::ReadOnly | QIODevice::Text)){ jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); int size = users.size(); for (int i=0;i<size;i++) { user = users.at(i).toObject(); if(user["username"] == sL[0] && user["password"] == sL[1]){ userobj = user; break; } } }else{ qDebug() << "open file failed"; } file.close(); jdoc.setObject(userobj); return jdoc.toJson(); } bool jsonexe::judgeJson(QString username) { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QJsonDocument jdoc = QJsonDocument::fromJson(file.readAll()); QJsonArray users = jdoc.array(); int size = users.size(); for (int i=0;i<size;i++) { QJsonObject user = users.at(i).toObject(); qDebug() << user["username"]; if(user["username"] == username){ return false; } } } file.close(); return true; } bool jsonexe::editJson(QStringList sL) { qDebug() << "EditJson" << sL; // 获取已存在的users if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "File opened failed"; return false; } jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); file.close(); // 验证信息,更改密码 QJsonArray newUsers; for (int i=0;i<users.size();i++) { QJsonObject userobj = users.at(i).toObject(); if(userobj["username"] == sL[0]){ if(sL.length() == 2){ QString acount = userobj["acount"].toString(); float total = acount.toFloat() + sL[1].toFloat(); userobj["acount"] = QString::number(total); qDebug() << "acount:" << userobj["acount"]; } else{ userobj["acount"] = sL[1]; userobj["score"] = sL[2]; userobj["password"] = sL[3]; } } newUsers.append(userobj); } //将newUsers重新写入文件 jdoc.setArray(newUsers); if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){ qDebug() << "File opened failed"; return false; } file.write(jdoc.toJson(QJsonDocument::Indented)); file.close(); return true; } bool jsonexe::appendJson(QStringList sL) { if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "File opened failed"; return false; } jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); file.close(); if(this->judgeJson(sL[0])){ QJsonObject userobj; userobj["username"] = sL[0]; userobj["password"] = sL[1]; userobj["type"] = sL[2]; userobj["acount"] = 0; userobj["score"] = 0; users.append(userobj); jdoc.setArray(users); file.open(QIODevice::WriteOnly|QIODevice::Text); file.write(jdoc.toJson(QJsonDocument::Indented)); file.close(); return true; }else{ return false; } }
时间: 2023-06-13 11:09:06 浏览: 49
这段代码是一个用于操作JSON文件的类jsonexe的实现。该类的构造函数初始化了JSON文件的路径,析构函数没有实现内容。
getJson方法可以获取JSON文件中对应用户名和密码的用户信息,并将其转换为JSON格式的数据,返回给调用者。
judgeJson方法用于判断用户名是否已存在于JSON文件中。
editJson方法用于修改JSON文件中的用户信息,可以更改密码、账户余额和积分。
appendJson用于向JSON文件中添加新用户信息。
该类的实现使用了Qt框架中的QJsonDocument、QJsonArray、QJsonObject等类来处理JSON数据。
相关问题
QString absolutePath = QDir(“../../../test.txt")
你给定的代码中,使用了Qt的QDir类和QDir构造函数来获取一个文件的绝对路径。构造函数中传入的字符串参数是一个相对路径,表示文件test.txt相对于当前工作目录的路径。
根据给定的相对路径"../../../test.txt",可以解释如下:
- ".." 表示返回上一级目录,因此"../../../test.txt"表示返回上三级目录,然后再进入test.txt文件。
- 当前工作目录是在构建应用程序时指定的,或者在运行时使用QCoreApplication::setApplicationDirPath()或QCoreApplication::addLibraryPath()等函数指定。
- 请注意,相对路径是相对于当前工作目录而言,所以它可能会随着应用程序运行环境的改变而变化。
该代码片段的作用是将相对路径转换为绝对路径,并将结果存储在QString类型的变量absolutePath中。
QString absolutePath = QDir(“../../../test.txt") 相关代码
这段代码使用了Qt Framework中的QDir和QString类。它的作用是获取相对路径"../../../test.txt"对应的绝对路径。
首先,我们创建了一个QString对象absolutePath来保存绝对路径。然后,我们使用QDir的构造函数来创建一个QDir对象,并将相对路径作为参数传递进去。
接下来,我们使用QDir的absoluteFilePath()函数来获取相对路径的绝对路径。这个函数会根据当前工作目录,将相对路径转换为绝对路径,并将其保存到absolutePath对象中。
最后,我们可以通过访问absolutePath对象来获取绝对路径。
注意:在使用QDir的构造函数时,可以省略相对路径中的"../"部分,直接使用"test.txt"来表示当前目录下的test.txt文件。