QT 解析 魔兽API返回Jsion数据

来源:互联网 发布:淘宝女装店哪个好 编辑:程序博客网 时间:2024/04/30 19:36
JsionParse::JsionParse(QString jsonStr ,QString doWhat,QString content_jsion)
{
    countryName = "";
    realmName = "";
    unionName = "";
    playerName = "";
    petSlots = "";
    pet_num = 0;
    mount_num = 0;
    achievementPoints = 0;
    this->jsonStr = jsonStr;
    this->doWhat = doWhat;
    if(doWhat == "realmForServerList"){
        countryName = content_jsion;
    }
    if(doWhat == "unionForPlayerList"){
        QStringList list = content_jsion.split("+");
        this->countryName = list.at(0);
        this->realmName = list.at(1);
        this->unionName = list.at(2);
    }
    if(doWhat == "playerForPetSLots"){
        QStringList list = content_jsion.split("+");
        this->countryName = list.at(0);
        this->realmName = list.at(1);
        this->unionName = list.at(2);
        this->playerName = list.at(2);
    }
}
void JsionParse::scanForInfo()
{
    QJsonArray subJsonarray;
    QString subFlags;
    /*
     *doWhat=  "realmForServerList" 扫服务器
     *         "unionForPlayerList" 扫工会的     成员列表
     *         "playerForInfo"      玩家基础信息 包含成就
     *         "playerForPetSLots"  宠物巣
     *         "playerForMounts"    坐骑
    */
    //qDebug() << "JsionParse(QString):" ;
    QJsonParseError jsonErr;
    QJsonDocument myjsonDoc1 = QJsonDocument::fromJson(jsonStr.toUtf8(),&jsonErr);
    if (jsonErr.error == QJsonParseError::NoError)
    {
        if (myjsonDoc1.isEmpty()){
            qDebug()<<"the json is empty!";
        }
        if (myjsonDoc1.isObject()){
            //qDebug()<<"json is object";
            QJsonObject jobj = myjsonDoc1.object();
            QStringList jsonkeylist = jobj.keys();
            qDebug()<<"jsonkeylist="<<jsonkeylist.count();
            QJsonObject::iterator it = jobj.begin();
            while(it != jobj.end())
            {
                //qDebug() << "------------------";
                switch (it.value().type())
                {
                    case QJsonValue::String:
                                {
                                    qDebug()<<"type is String!";
                                    if(doWhat=="playerForInfo" && it.key()=="name"){
                                        playerName = it.value().toDouble();
                                        //qDebug()<< "playerName = " << playerName;
                                    }
                                    qDebug()<<it.key()<<"="<< it.value().toString();
                                    break;
                                }
                    case QJsonValue::Array:
                                {
                                    qDebug()<<"type is array!";
                                    subFlags = it.key();
                                    /*
                                     *flags判断:  petSlots = 战斗宠物列表
                                     *           members = 工会成员列表
                                     *           "news" = 工会新闻列表
                                    */
                                    //底层数量,如 宠物槽3 人员400 坐骑?
                                    subJsonarray = it.value().toArray();
                                    qDebug() << "flags="<<subFlags << "count="<<subJsonarray.count()<<endl ;
                                    //qDebug()<<subJsonarray;
                                    //qDebug()<<"index 1 ="<<subarray.at(0).toDouble();
                                    //qDebug()<<"index 2 ="<<subarray.at(1).toString();
                                    //qDebug()<<"index 3 ="<<subarray.at(2).toBool();
                                    break;
                                }
                    case QJsonValue::Bool:
                                //qDebug()<<"type is bool!";
                                //qDebug()<<it.key()<<"="<<it.value().toBool();
                                break;
                    case QJsonValue::Double:
                                {
                                    qDebug()<<"type is Double!";
                                    //成就点 工会or个人需判断
                                    if(it.key()=="achievementPoints"){
                                        achievementPoints = it.value().toDouble();
                                    }
                                    //服务器组
                                    if(it.key() =="battlegroup"){
                                    }
                                    //阵营
                                    if(it.key() =="side"){
                                        //side = 1 部落 0 联盟
                                    }
                                    //等级
                                    if(it.key() =="level"){
                                        //工会等级 人物等级
                                    }
                                    //种族 2 血精灵
                                    if(it.key() =="class"){
                                    }
                                    //性别 1=
                                    if(it.key() =="gender"){
                                    }
                                    qDebug()<<it.key()<<"="<<it.value().toDouble();
                                    break;
                                }
                    case QJsonValue::Object:
                                {
                                    qDebug()<<"type is Object!";
                                /*
                                 *      "mounts": =坐骑表 Object
                                 *         pets   =战斗宠物列表 Object
                                */
                                    if(it.key() == "pets"){
                                        pet_num = it.value().toObject().value("numCollected").toDouble();
                                    }
                                    if(it.key() == "mounts"){
                                        mount_num = it.value().toObject().value("numCollected").toDouble();
                                    }
                                    qDebug()<<it.key()<<"="<<it.value().toObject();
                                    break;
                                }
                    case QJsonValue::Null:
                                qDebug()<<"type is null!";
                                qDebug()<<it.key()<<"=null";
                                break;
                    case QJsonValue::Undefined:
                                qDebug()<<"type is Undefined!";
                                break;
                }
                it++;
            }
        }
    }
    if(doWhat == "realmForServerList"){
        for(int i=0;i<subJsonarray.count();i++)
        {
            if(subFlags == "realms"){
                qDebug() << subJsonarray.at(i).toObject().value("battlegroup").toString();
                QString type  = subJsonarray.at(i).toObject().value("type").toString();
                realmName     = subJsonarray.at(i).toObject().value("name").toString();
                QString group = subJsonarray.at(i).toObject().value("battlegroup").toString();
                QString stringR =  realmName + "+" + type + "+" + group; //构造特定格式 emit
                emit realmForServerList_Return(stringR);
            }
        }
    }
    if(doWhat == "unionForPlayerList"){
        QJsonObject::iterator it_sub;
        for(int i=0;i<subJsonarray.count();i++)
        {
            if(subFlags == "members"){
                it_sub = subJsonarray.at(i).toObject().value("character").toObject().find("name");
                playerName = it_sub.value().toString();
                QString stringR = countryName+"_"+realmName + "+" + unionName+ "+" + playerName; //构造特定格式 emit
                emit unionForPlayerList_Return(stringR);
            }
        }
    }
    if(doWhat == "playerForPetSLots"){
        QJsonObject::iterator it_sub;
        QJsonArray petslots_subJsonarray;
        for(int i=0;i<subJsonarray.count();i++)
        {
            if(subFlags == "petSlots"){
                it_sub = subJsonarray.at(i).toObject().find("battlePetGuid");
                petSlots += (it_sub.value().toString()+"/");
                petslots_subJsonarray = subJsonarray.at(i).toObject().value("abilities").toArray();
                for(int j=0;j<petslots_subJsonarray.count();j++){
                    //qDebug()<< "--" << petslots_subJsonarray.at(j).toDouble();
                    petSlots +=  QString::number(petslots_subJsonarray.at(j).toDouble())+"/";
                }
                emit playerForPetSLots_Return(petSlots);
            }
        }
    }
    if(doWhat == "playerForInfo")
    {
        emit playerForInfo_Return(achievementPoints);
    }
    if(doWhat == "playerForPet")
    {
        emit playerForPet_Return(pet_num);
    }
    if(doWhat == "playerForMounts")
    {
        emit playerForMounts_Return(mount_num);
    }
}


0 0
原创粉丝点击