qjson 读写

读:

json:

{
    "first":
    {
        "second":
        {
            "third1":"23423",
            "third2":"2323"
        },
        "four":"234234"
    },
    "five":"34234"
}

代码:

const QString Results = "first";
const QString BatData = "second";
const QString BatVal = "third1";

void ParseJson(QString recv)
{
    QJsonParseError jsonError;

    QJsonDocument doucment = QJsonDocument::fromJson(recv.toStdString().data(), &jsonError);  // 转化为 JSON 文档
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
    {  // 解析未发生错误
        if (doucment.isObject())
        { // JSON 文档为对象
            QJsonObject object = doucment.object();  // 转化为对象
            if (object.contains(Results)) {
                QJsonObject resultsObj = object[Results].toObject();// 多级非数组
                if (resultsObj.contains(BatData)) {
                    QJsonObject batDataObj = resultsObj[BatData].toObject();
                    if (batDataObj.contains(BatVal)) {
                        int nBatVal = batDataObj[BatVal].toInt();
                        
                    }
                }
            }
        }
    }
}

 

posted @ 2022-03-14 19:17  阳光下的小土豆  阅读(197)  评论(0编辑  收藏  举报