Qt对Json文件的读写

本文介绍了一种从JSON文件中读取配置信息并将其解析为可操作数据的方法,同时详细展示了如何将数据结构重新写入JSON文件的过程。通过具体的代码示例,读者可以学习到如何使用Qt库进行JSON数据的读写操作。
摘要由CSDN通过智能技术生成

示例Json文件:HWCfg.json

[
    {
        "Cameras": [
        ],
        "NO": "00000000000000000000",
        "Siip": "127.0.0.1",
        "Siport": 8333
    },
    {
        "Cameras": [
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            },
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            }
        ],
        "NO": "00000000000000000001",
        "Siip": "127.0.0.1",
        "Siport": 8333
    },
    {
        "Cameras": [
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            },
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            },
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            }
        ],
        "NO": "00000000000000000002",
        "Siip": "127.0.0.1",
        "Siport": 8333
    },
    {
        "Cameras": [
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            },
            {
                "Acc": "admin",
                "Ip": "192.168.0.1",
                "Port": 554,
                "Type": 0,
                "pwd": "samples123"
            }
        ],
        "NO": "00000000000000000003",
        "Siip": "127.0.0.1",
        "Siport": 8333
    }
]

读取解析Json文件:

void PassageWayMgr::readCfg()
{
    QString passageWayNo = "";
    QString passageWaySiIp = "";
    int passageWaySiPort = 0;
    QVector<ICamera *> passageWayCameras;

    QFile HwCfgFile("HWCfg.json");
    if(!HwCfgFile.open(QIODevice::ReadOnly))
    {
        FLOGW("[%s]HwCfg.json File Open Failed.", __FUNCTION__);
        return;
    }
    QByteArray ba = HwCfgFile.readAll();
    HwCfgFile.close();

    QJsonParseError e;
    QJsonDocument doc = QJsonDocument::fromJson(ba, &e);
    if(e.error != QJsonParseError::NoError || doc.isNull())
    {
        FLOGW("[%s]HwCfg.json parse Failed.", __FUNCTION__);
        return;
    }

    if (doc.isArray()) {
        QJsonArray passageWayArr = doc.array();
        for (int i = 0; i < passageWayArr.size(); i++) {
            passageWayCameras.clear(); // 清楚前一次遗留
            QJsonValue passageWay = passageWayArr.at(i);
            if (passageWay.isObject()) {
                QJsonObject passageWayObj = passageWay.toObject();
                if (passageWayObj.contains(JSONKEY_PASSAGEWAYNO)) {
                    passageWayNo = passageWayObj.take(JSONKEY_PASSAGEWAYNO).toString();
                }
                if (passageWayObj.contains(JSONKEY_PASSAGEWAYSIIP)) {
                    passageWaySiIp = passageWayObj.take(JSONKEY_PASSAGEWAYSIIP).toString();
                }
                if (passageWayObj.contains(JSONKEY_PASSAGEWAYSIPORT)) {
                    passageWaySiPort = passageWayObj.take(JSONKEY_PASSAGEWAYSIPORT).toInt();
                }
                if (passageWayObj.contains(JSONKEY_CAMERAS)) {
                    QJsonValue camerasValue = passageWayObj.take(JSONKEY_CAMERAS);
                    if (camerasValue.isArray()) {
                        QJsonArray camerasArr = camerasValue.toArray();
                        for (int j = 0; j < camerasArr.size(); j++) {
                            QString camIp = "";
                            int camPort = 0;
                            QString camAcc = "";
                            QString camPwd = "";
                            int camType = 0;

                            QJsonValue camera = camerasArr.at(j);
                            if (camera.isObject()) {
                                QJsonObject cameraObj = camera.toObject();
                                if (cameraObj.contains(JSONKEY_CAMERAACC)) {
                                    camAcc = cameraObj.take(JSONKEY_CAMERAACC).toString();
                                }
                                if (cameraObj.contains(JSONKEY_CAMERAPWD)) {
                                    camPwd = cameraObj.take(JSONKEY_CAMERAPWD).toString();
                                }
                                if (cameraObj.contains(JSONKEY_CAMERAIP)) {
                                    camIp = cameraObj.take(JSONKEY_CAMERAIP).toString();
                                }
                                if (cameraObj.contains(JSONKEY_CAMERAPORT)) {
                                    camPort = cameraObj.take(JSONKEY_CAMERAPORT).toInt();
                                }
                                if (cameraObj.contains(JSONKEY_CAMERATYPE)) {
                                    camType = cameraObj.take(JSONKEY_CAMERATYPE).toInt();
                                }
                            }

                            // 构造相机
                            ICamera *pCamera = cameraCreate(camIp, camPort, camAcc, camPwd, camType);
                            if (NULL != pCamera) {
                                passageWayCameras.push_back(pCamera);
                            }
                        }
                    }
                }
            }

            // 构造通道
            PassageWay *way = new PassageWay(passageWayNo, passageWaySiIp, passageWaySiPort);
            way->addCameras(passageWayCameras);

            PPASSAGEWAYMGR->addPassageWay(way);
        }
    } else {
        FLOGW("[%s]HwCfg.json doc is not array.", __FUNCTION__);
        return;
    }
}

写入Json文件:

void PassageWayMgr::writeCfg()
{
    QJsonArray passageWayArr;
    QJsonObject passageWayObj;
    for (int i = 0; i < _pwVec.size(); i++) {
        passageWayObj.insert(JSONKEY_PASSAGEWAYNO, _pwVec[i]->getNo());
        passageWayObj.insert(JSONKEY_PASSAGEWAYSIIP, _pwVec[i]->getSiip());
        passageWayObj.insert(JSONKEY_PASSAGEWAYSIPORT, _pwVec[i]->getSiport());

        QVector<ICamera *> cameras = _pwVec[i]->getCameras();
        QJsonArray camerasArr;
        for (int j = 0; j < cameras.size(); j++) {
            QJsonObject cameraObjs;
            cameraObjs.insert(JSONKEY_CAMERAIP, cameras[j]->getIp());
            cameraObjs.insert(JSONKEY_CAMERAPORT, cameras[j]->getPort());
            cameraObjs.insert(JSONKEY_CAMERAACC, cameras[j]->getAccount());
            cameraObjs.insert(JSONKEY_CAMERAPWD, cameras[j]->getPasswd());
            cameraObjs.insert(JSONKEY_CAMERATYPE, (int)cameras[j]->getType());
            camerasArr.push_back(cameraObjs);
        }

        passageWayObj.insert(JSONKEY_CAMERAS, camerasArr);

        passageWayArr.push_back(passageWayObj);
    }

    QJsonDocument doc(passageWayArr);
    QByteArray ba = doc.toJson(); // 转换成二进制数据
    QFile HwCfgFile("HWCfg.json");
    if (!HwCfgFile.open(QIODevice::WriteOnly)) {
        FLOGW("[%s]HwCfg.json File Open Failed.", __FUNCTION__);
        return;
    }
    HwCfgFile.write(ba);
    HwCfgFile.close();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值