Qt简单的解析Json数据例子(一)
阅读原文时间:2023年07月15日阅读:1

要解析的json的格式为:

{
    "rootpath": "001",
    "usernum": 111,
    "childdep": [
        {
            "depid": "11",
            "depnum": 20
        },
        {
            "depid": "15",
            "depnum": 23
        }
    ]
}

std::map<std::wstring, int> part_depid_num;

    //这里是吧json的数据直接写成了字符串的形式来完成
    QByteArray data = QByteArray("{\"rootpath\":\"001\",\"usernum\":111,\"childdep\":[{\"depid\":\"11\",\"depnum\":20},{\"depid\":\"15\",\"depnum\":23}]}");
    //判断字符串转化为QJsonDocument  是否出现了错误
    QJsonParseError jsonError;//Qt5新类
    QJsonDocument json = QJsonDocument::fromJson(data, &jsonError);//Qt5新类
    if (jsonError.error == QJsonParseError::NoError)
    {
        if (json.isObject())
        {
            QJsonObject rootObj = json.object();
            QString rootpath;
            int rootusernum;
            //是否含有key  rootpath
            if (rootObj.contains("rootpath"))
            {
                //取出key为rootpath的值
                QJsonValue value = rootObj.value("rootpath");
                //判断是否是string类型
                if (value.isString())
                {
                    rootpath = value.toString();
                }
            }
            if (rootObj.contains("usernum"))
            {
                //取出key为usernum的值
                QJsonValue value = rootObj.value("usernum");
                //判断是否为double类型
                if (value.isDouble())
                {
                    rootusernum = value.toDouble();
                }
            }
            part_depid_num[rootpath.toStdWString()] = rootusernum;
            if (rootObj.contains("childdep"))
            {
                QJsonValue valueArray = rootObj.value("childdep");
                //判断类型是否为array,并且将array遍历出来
                if (valueArray.isArray())
                {
                    QJsonArray jsonArray = valueArray.toArray();
                    for (int i = 0; i < jsonArray.count();i++)
                    {
                        QJsonValue childValue = jsonArray[i];

                        if (childValue.isObject())
                        {
                            QString child_depid;
                            QString child_usernum;
                            int child_usern;
                            QJsonObject  childObject = childValue.toObject();
                            if (childObject.contains("depid"))
                            {
                                QJsonValue valueJson = childObject.value("depid");
                                if (valueJson.isString())
                                {
                                    child_depid = valueJson.toString();
                                }
                            }
                            if (childObject.contains("depnum"))
                            {
                                QJsonValue valueJson = childObject.value("depnum");
                                if (valueJson.isDouble())
                                {
                                    child_usern = valueJson.toDouble();
                                }
                            }
                            if (child_usernum.isEmpty())
                            {
                                part_depid_num[child_depid.toStdWString()] = child_usern;
                            }
                        }
                    }
                }
            }
        }
    }
    //printf(QString::fromStdWString(part_depid_num.begin()->first).toStdString().c_str());
    std::map<std::wstring, int> ::iterator it;
    for (it = part_depid_num.begin(); it != part_depid_num.end();it++)
    {
        cout << QString::fromStdWString(it->first).toStdString().c_str()<<endl;
    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章