zoukankan      html  css  js  c++  java
  • Qt简单的解析Json数据例子(一)

    要解析的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;
        }
  • 相关阅读:
    NOI2010 超级钢琴
    [linux][nginx] 常用2
    [linux][nginx] 常用
    [PHP]听说随机数mt_rand()比rand()速度快,闲的无聊测试了一下!
    [linux] 权限问题
    [Laravel] 自带分页实现以及links方法不存在错误
    [YII2] 去除自带js,加载自己的JS,然后ajax(json)传值接值!
    [PHP]PHP设计模式:单例模式
    [html]浏览器标签小图标LOGO简单设置
    [javascript]JS获取当前时间戳的方法
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14431807.html
Copyright © 2011-2022 走看看