示例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();
}