当前位置: 首页 > 工具软件 > Json_LAMP > 使用案例 >

Qt组装json与解析json

宁浩博
2023-12-01

首先是组装成json格式

     QString timeStr = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
        QJsonDocument door_jDoc;
        //door
        QJsonObject door_jObject1;
        door_jObject1.insert("door_name", "01");
        door_jObject1.insert("door_value", "1");
        door_jObject1.insert("door_camera", "1");
        door_jObject1.insert("door_lamp", "1");
        door_jObject1.insert("door_lock", "1");

        QJsonObject door_jObject2;
        door_jObject2.insert("door_name", "02");
        door_jObject2.insert("door_value", "1");

        QJsonObject door_jObject3;
        door_jObject3.insert("door_name", "03");
        door_jObject3.insert("door_value", "1");

        QJsonArray door_jarray;//json数组
        door_jarray.push_back(door_jObject1);
        door_jarray.push_back(door_jObject2);
        door_jarray.push_back(door_jObject3);
        qDebug()<<"door_jarray=="<<door_jarray;

        QJsonObject jObject;
        QJsonDocument jDoc;
        QString t_timeStr = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
        QString midStr = "1515"+ t_timeStr;
        jObject.insert("mid", midStr);
        jObject.insert("type", "CMD_DEVICE_CONTROL_DOOR");
        jObject.insert("time", timeStr);
        jObject.insert("imei", t_imei);
        jObject.insert("door", door_jarray);//添加json数组
        jObject.insert("wait", "15");
        jObject.insert("name", "江阴市新园路 6 号");
        jObject.insert("video", "1");
        jObject.insert("operator", "000123456789");
        jObject.insert("operator_type", "1");
        jDoc.setObject(jObject);

        qInfo() << "object json:" << QString(jDoc.toJson(QJsonDocument::Compact));
        QString sendStr = QString(jDoc.toJson(QJsonDocument::Compact));

接着是解析

void MainWidget::analysisReadMsgC(QByteArray strmsg)
{
    QJsonParseError parseJsonErr;
    QJsonDocument document;
    document = QJsonDocument::fromJson(strmsg,&parseJsonErr);
    if(parseJsonErr.error != QJsonParseError::NoError){
        qDebug()<<"analysisReadMsgC json error:"<<parseJsonErr.error;
        return ;
    }else{
        QJsonObject jsonObject  = document.object();
        QString mid = jsonObject["mid"].toString();       
        QString type = jsonObject["type"].toString();        
        QString time = jsonObject["time"].toString();
        QString imei = jsonObject["imei"].toString();      
        QString iccid = jsonObject["iccid"].toString();        
        QString hard_ver = jsonObject["hard_ver"].toString();
        QString soft_ver = jsonObject["soft_ver"].toString();
        QString name = jsonObject["name"].toString();
        QJsonArray door = jsonObject["door"].toArray();//解析数组
        if(door.size()>=3){
            QJsonObject doorObject1 = door.at(0).toObject();
            QJsonObject doorObject2 = door.at(1).toObject();
            QJsonObject doorObject3 = door.at(2).toObject();

            QString door_name1 = doorObject1["door_name"].toString();
            QString door_value1 = doorObject1["door_value"].toString();        
            QString door_name2 = doorObject2["door_name"].toString();
            QString door_value2 = doorObject2["door_value"].toString();
            QString door_name3 = doorObject3["door_name"].toString();
            QString door_value3 = doorObject3["door_value"].toString();
        }
 类似资料: