QJson生成文件和解析文件

QJson生成.json文件

生成简单的json文件

    QVariantHash data;
    QVariantHash patientData;
    patientData.insert("patient ID","22334");
    patientData.insert("Name","ZYL_YY");
    patientData.insert("age","26");
    patientData.insert("clinicalData","wwwww");
    patientData.insert("Selected site","lever");
    patientData.insert("Selected mode","pulse mode");
    data.insert("patientInformation",patientData);
     QJsonObject rootobj = QJsonObject::fromVariantHash(data);
    QJsonDocument document;
    document.setObject(rootobj);
    QByteArray byte_array = document.toJson();
    QString json_str = (byte_array);
    QFile file("/home/wxh1/1.json");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate))
    {
        qDebug()<<"file error";
    }
    QTextStream in(&file);
    in<

生成的jison文件

{
    "patientInformation": {
        "Name": "ZYL_YY",
        "Selected mode": "pulse mode",
        "Selected site": "lever",
        "age": "26",
        "clinicalData": "wwwww",
        "patient ID": "22334"
    }
}

解析json文件QFile file("/home/wxh1/1.json");

//
file.open(QIODevice::ReadOnly | QIODevice::Text);
//

QString value = file.readAll(); file.close(); // QJsonParseError jsonError; QJsonDocument
doucement = QJsonDocument::fromJson(value.toUtf8(),&jsonError); // if(!doucement.isNull()&&(jsonError.error == QJsonParseError::NoError)) { // if(doucement.isObject()) { // _jsonObject = doucement.object(); QVector patien_item(6); // if(_jsonObject.contains("patientInformation")) { // QJsonValue jsonValueList = _jsonObject.value(QStringLiteral("patientInformation")); QJsonObject item = jsonValueList.toObject(); // patien_item[0]=item["Name"].toString(); patien_item[1]=item["Selected mode"].toString(); patien_item[2]=item["Selected site"].toString(); patien_item[3]=item["patient ID"].toString(); patien_item[4]=item["age"].toString(); patien_item[5]=item["clinicalData"].toString(); } } }

构建json数组

  QJsonArray probePlacementData;
    for(int i =0;i<5;i++)
    {
        QJsonObject obj;
        obj.insert("voltage","3000");
        obj.insert("distance","2.4");
        obj.insert("pulseLength","100");
        obj.insert("numpulse","90");
        probePlacementData.append(obj);

    }
    data.insert("probePlacementData",probePlacementData);

    QJsonObject rootobj = QJsonObject::fromVariantHash(data);
    QJsonDocument document;
    document.setObject(rootobj);
    QByteArray byte_array = document.toJson();
    QString json_str = (byte_array);
    QFile file("/home/wxh1/1.json");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate))
    {
        qDebug()<<"file error";
    }
    QTextStream in(&file);
    in<

 生成的json文件

{
    "probePlacementData": [
        {
            "distance": "2.4",
            "numpulse": "90",
            "pulseLength": "100",
            "voltage": "3000"
        },
        {
            "distance": "2.4",
            "numpulse": "90",
            "pulseLength": "100",
            "voltage": "3000"
        },
        {
            "distance": "2.4",
            "numpulse": "90",
            "pulseLength": "100",
            "voltage": "3000"
        },
        {
            "distance": "2.4",
            "numpulse": "90",
            "pulseLength": "100",
            "voltage": "3000"
        },
        {
            "distance": "2.4",
            "numpulse": "90",
            "pulseLength": "100",
            "voltage": "3000"
        }
    ]
}

 解析json文件

struct _probePlacementDataStruct
{
    QString voltage;
    QString distance;
    QString pulseLegth;
    QString numpulse;
};


QFile file("/home/wxh1/1.json");
//
file.open(QIODevice::ReadOnly | QIODevice::Text);
//

QString value = file.readAll(); file.close(); // QJsonParseError jsonError; QJsonDocument doucement = QJsonDocument::fromJson(value.toUtf8(),&jsonError); // if(!doucement.isNull()&&(jsonError.error == QJsonParseError::NoError)) { // if(doucement.isObject()) { // _jsonObject = doucement.object(); if(_jsonObject.contains(QStringLiteral("probePlacementData"))) { QJsonValue arrayVaule = _jsonObject.value(QStringLiteral("probePlacementData")); if(arrayVaule.isArray()) { QJsonArray array = arrayVaule.toArray(); for(int i = 0;i

  

 

转载于:https://www.cnblogs.com/wxh-53919/p/10910495.html

你可能感兴趣的:(QJson生成文件和解析文件)