1. 程式人生 > >qt json操作

qt json操作

bool oar qfile remove ret ado lose 收藏 append

json文件結構形式

{
"xiao1": [
{
"000100": {
"industryCode": "000100",
"industryName": "test"
}
},
{
"000101": {
"industryCode": "000100",
"industryName": "test"
}
},
{
"000103": {

"industryCode": "000100",
"industryName": "test"
}
}
],
"xiaogdsadg": [
{
"12321344": {
"industryCode": "12321344",
"industryName": "454555"
}
},
{
"54545445": {
"industryCode": "54545445",
"industryName": "454555"
}
},
{
"454454654": {
"industryCode": "454454654",
"industryName": "454555"
}
}
]
}

//判斷指定項是否在json文件裏

bool MainWindow:: isInJson(QString userName,QString rowId)
{
    QString val;
    QFile file;
    file.setFileName("F:\\work\\qtProjects\\untitled11\\test123.json");
    file.open(QIODevice::ReadOnly| QIODevice::Text);
    val = file.readAll();
    file.close();
    QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
    QJsonObject objTop1 = doc.object();
    QStringList keys = objTop1.keys();
    //用戶循環
    for(int i = 0; i < keys.count();i++)
    {
        if(keys.at(i)!= userName)
            continue;
        QJsonArray arrayRows = objTop1.value(keys.at(i)).toArray();
        //行循環
        for(int j =0;j < arrayRows.count();j++)
        {
            QJsonObject objRow = arrayRows.at(j).toObject();
            if(objRow.keys().at(0) == rowId)
            {
                return true;
            }
        }
    }
    return false;
}
//添加收藏
void MainWindow::addJson(QString userName, QString rowId)
{
    if(!isInJson(userName,rowId))
    {
        bool hasUser = false;
        QString val;
        QFile file;
        file.setFileName("F:\\work\\qtProjects\\untitled11\\test123.json");
        file.open(QIODevice::ReadOnly| QIODevice::Text);
        val = file.readAll();
        file.close();
        QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
        QJsonObject objTop1 = doc.object();
        QStringList keys = objTop1.keys();
        //用戶循環
        for(int i = 0; i < keys.count();i++)
        {
            if(keys.at(i) != userName)
                continue;
            QJsonArray arrayRows = objTop1.value(keys.at(i)).toArray();
            QJsonObject addObj;
            QJsonObject addObj1;
            addObj1.insert("industryCode",rowId);
            addObj1.insert("industryName","454555");
            addObj.insert(rowId,addObj1);
            arrayRows.append(addObj);
            objTop1.insert(userName,arrayRows);
            hasUser = true;
            break;
        }
        //該用戶還沒有添加
        if(!hasUser)
        {
            QJsonArray arrayRows;
            QJsonObject addObj;
            QJsonObject addObj1;
            addObj1.insert("industryCode",rowId);
            addObj1.insert("industryName","444545");
            addObj.insert(rowId,addObj1);
            arrayRows.append(addObj);
            objTop1.insert(userName,arrayRows);
        }
        doc.setObject(objTop1);
        file.setFileName("F:\\work\\qtProjects\\untitled11\\test123.json");
        file.open(QIODevice::WriteOnly| QIODevice::Text);
        file.write(doc.toJson());
        file.close();
    }
}
//取消收藏
void MainWindow::removeJson(QString userName, QString rowId)
{
    if(isInJson(userName,rowId))
    {
        QString val;
        QFile file;
        file.setFileName("F:\\work\\qtProjects\\untitled11\\test123.json");
        file.open(QIODevice::ReadOnly| QIODevice::Text);
        val = file.readAll();
        file.close();
        QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
        QJsonObject objTop1 = doc.object();
        QStringList keys = objTop1.keys();
        //用戶循環
        for(int i = 0; i < keys.count();i++)
        {
            if(keys.at(i) != userName)
                continue;
            QJsonArray arrayRows = objTop1.value(keys.at(i)).toArray();
            //行循環
            for(int j =0;j < arrayRows.count();j++)
            {
                QJsonObject objRow = arrayRows.at(j).toObject();
                if(objRow.keys().at(0) == rowId)
                {
                    arrayRows.removeAt(j);
                    objTop1.insert(userName,arrayRows);
                    break;
                }
            }
            doc.setObject(objTop1);
            file.setFileName("F:\\work\\qtProjects\\untitled11\\test123.json");
            file.open(QIODevice::WriteOnly| QIODevice::Text);
            file.write(doc.toJson());
            file.close();
            break;
        }

    }
}

qt json操作