QJson生成文件和解析文件

Posted wxh-53919

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<<json_str;
    file.close();
  

生成的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<QString> 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_str;
    file.close();

 生成的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<array.size();i++) { QJsonValue iconArray = array.at(i); QJsonObject childrens = iconArray.toObject(); QString pAdd = childrens["P+"].toString(); QString pdec = childrens["P-"].toString(); QString voltage = childrens["voltage"].toString(); QString distance = childrens["distance"].toString(); QString pulseLength = childrens["pulseLength"].toString(); QString numPulse = childrens["numpulse"].toString(); QString v_cm = childrens["v/cm"].toString(); _probePlacementDataStruct list; list.voltage = voltage; list.distance = distance; list.pulseLegth = pulseLength; list.numpulse = numPulse; } } } } }

  

 

以上是关于QJson生成文件和解析文件的主要内容,如果未能解决你的问题,请参考以下文章

QtJson用Qt自带的QJson,直接一步到位封装和解析一个类的实例对象!

QT开发(六十二)———QT5解析Json文件

Qt平台下使用QJson解析和构建JSON字符串

Qt平台下使用QJson解析和构建JSON字符串

Qt平台下使用QJson解析和构建JSON字符串

Qt--解析Json