1. 在數據編輯器中導入Excel文件json
2. 導出Json文件,選擇屬性模式api
3. 使用文本編輯器查看Json數據編輯器
4. 在程序中添加Json解析函數ide
void getJsonFromFile(const char* fileName, rapidjson::Document &jsonDict) { unsigned char *pBytes = NULL; std::string jsonpath; jsonpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName); unsigned long size = 0; pBytes = CCFileUtils::sharedFileUtils()->getFileData(jsonpath.c_str(), "r" , &size); CCData *data = new CCData(pBytes, size); std::string load_str = std::string((const char *)data->getBytes(), data->getSize() ); CC_SAFE_DELETE(data); jsonDict.Parse<0>(load_str.c_str()); }
5. 獲取Json文件中的數據函數
rapidjson::Document rapidJsonData; getJsonFromFile("data.json", rapidJsonData); for(int i = 0; i < 4; i++) { const char* name = DictionaryHelper::shareHelper()->getStringValue_json(rapidJsonData[i], "name"); int level = DictionaryHelper::shareHelper()->getIntValue_json(rapidJsonData[i], "level"); int hp = DictionaryHelper::shareHelper()->getIntValue_json(rapidJsonData[i], "hp"); int attack = DictionaryHelper::shareHelper()->getIntValue_json(rapidJsonData[i], "attack"); CCLog("%s, %d, %d, %d", name, level, hp, attack); }
6. 程序運行結果jsonp