cocos2dx学习之rapidjson库json数据解析

本地json文件数据:

{
  "dialogs": [
    {
      "tag": "1020",
      "title": "重置应用",
      "text": "您的观看记录与追剧收藏都将被删除,\n您确定要重置吗?",
      "gap": "50",
      "isdisableback": "0",
      "isprogress": "0",
      "isloading": "0",
      "buttons": [
        {
          "name": "确定"
        },
        {
          "name": "取消"
        }
      ]
    },
    {
      "tag": "1021",
      "title": "网络异常",
      "text": "网络连接异常!",
      "gap": "50",
      "isdisableback": "0",
      "isprogress": "0",
      "isloading": "0",
      "buttons": [
        {
          "name": "刷新重试"
        },
        {
          "name": "网络设置"
        }
      ]
    }
  ]
}


解析:

void MiDialogFrame::parseJsonFromFile(){
    rapidjson::Document document;
    bool bRet = false;
    unsigned long bufferSize = 0;
    unsigned char *pBytes = NULL;
    do {
        pBytes = CCFileUtils::sharedFileUtils()->getFileData("dialog_mi.json", "r", &bufferSize);
        if(pBytes == NULL || strcmp((char *)pBytes, "") == 0){
            return;
        }
        std::string load_str((const char *)pBytes, bufferSize);
        CC_SAFE_DELETE_ARRAY(pBytes);
        document.Parse<0>(load_str.c_str());
        CC_BREAK_IF(document.HasParseError() || !document.IsObject() || !document.HasMember("dialogs"));
        RAPIDJSON_ASSERT(document.HasMember("dialogs"));
        const rapidjson::Value &pDialogArr = document["dialogs"];
        if (!pDialogArr.IsArray()) {
            return;
        }
        for (rapidjson::SizeType i = 0; i < pDialogArr.Size(); i++) {
            const rapidjson::Value &dialog = pDialogArr[i];
            int tag = -1;
            if (dialog.HasMember("tag") && dialog["tag"].IsString()) {
                tag = atoi(dialog["tag"].GetString());
                if (tag == m_pMiDialogType) {
                    if (dialog.HasMember("title") && dialog["title"].IsString()) {
                        m_strTitle = dialog["title"].GetString();
                    }
                    if (dialog.HasMember("text") && dialog["text"].IsString()) {
                        m_strText = dialog["text"].GetString();
                    }
                    if (dialog.HasMember("gap") && dialog["gap"].IsString()) {
                        m_nButtonGap = atoi(dialog["gap"].GetString());
                    }
                    if (dialog.HasMember("isdisableback") && dialog["isdisableback"].IsString()) {
                        const rapidjson::Value &isdisableback = dialog["isdisableback"];
                        m_bDisableBack = atoi(isdisableback.GetString()) == 1;
                    }
                    if (dialog.HasMember("isprogress") && dialog["isprogress"].IsString()) {
                        if (atoi(dialog["isprogress"].GetString()) == 1) {
                            createProgress();
                        }
                    }
                    if (dialog.HasMember("isloading") && dialog["isloading"].IsString()) {
                        if (atoi(dialog["isloading"].GetString()) == 1) {
                            createLoading();
                        }
                    }
                    if (dialog.HasMember("buttons") && dialog["buttons"].IsArray()) {
                        const rapidjson::Value &pButtonArr = dialog["buttons"];
                        for (rapidjson::SizeType j = 0; j < pButtonArr.Size(); j++) {
                            const rapidjson::Value &button = pButtonArr[j];
                            if (button.HasMember("name") && button["name"].IsString()) {
                                m_arrButtonTitles->addObject(ccs(button["name"].GetString()));
                            }
                        }
                    }
                    break;
                }
            }
            
        }
        bRet = true;
    } while (0);
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值