• qt json操作


    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;
            }
    
    
        }
    }
  • 相关阅读:
    linux 内核防火墙配置规则
    postfix 配置邮件服务器
    JDK 和 tomcat 安装 配置
    ifstat-网络接口监测工具
    一个比较通用的Makefile
    [转]vim常用命令
    [转]Valgrind简单用法
    [转]UDP/TCP穿越NAT的P2P通信方法研究(UDP/TCP打洞 Hole Punching)
    [转]Reed Solomon纠删码
    [转]分布式文件系统FastDFS架构剖析
  • 原文地址:https://www.cnblogs.com/tianmochou/p/9264430.html
Copyright © 2020-2023  润新知