本地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);
}