容我直接上代码
/** * 保存Json文件 * * @param fileName 文件名 * @param doc Json对象 * * @return 是否保存成功 */ static bool saveJsonFile(const char *fileName,rapidjson::Document &doc) { bool bRet = false; do { //LS_LOG("file path : %s",fileName); rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); doc.Accept(writer); std::string str = buffer.GetString(); FILE *fp = std::fopen(lsStandardPath(fileName).c_str(),"wb"); CCASSERT(fp != nullptr,"file open error"); //fwrite(str.c_str(),str.length(),1,fp); fputs(str.c_str(),fp); fclose(fp); bRet = true; } while (0); return bRet; } /** * 删除Json数组的某一个对象 * * @param index 第几个 * @param value Json对象 * * @return 是否读取成功 */ static bool removeJsonArrayItem(int index,rapidjson::Value &value) { bool bRet = false; int size = value.Size(); if (index < size) { for (unsigned int i = index; i + 1 < value.Size(); i++) value[i] = value[i + 1]; value.PopBack(); bRet = true; } return bRet; } /** * 将char*转成Json对象 * * @param str 要转换的字符 * @param doc 转换后的Json对象 * * @return 是否转换成功 */ static bool readJsonWithString(const char *str,rapidjson::Document &doc) { bool bRet = false; do { std::string content(str); replaceString(content,":null",":\"\""); doc.Parse<0>(content.c_str()); CCASSERT(!doc.HasParseError(),"HasParseError"); bRet = true; } while (0); return bRet; } /** * 读取Json文件 * * @param fileName 文件名 * @param doc 转换后的Json对象 * * @return 是否读取成功 */ static bool readJsonWithFile(const char *fileName,rapidjson::Document &doc) { bool bRet = false; if (cocos2d::FileUtils::getInstance()->isFileExist(fileName)) { std::string contentStr = cocos2d::FileUtils::getInstance()->getStringFromFile(fileName); //LS_LOG("%s connent:%s",fileName,contentStr.c_str()); bRet = readJsonWithString(contentStr.c_str(),doc); } return bRet; } /** * 将json的Value转成String * * @param node Json格式的Value * @param strret 转换后的String * * @return 0表示成功,-1表示失败 */ static int valueToString(const rapidjson::Value &node,std::string &strret) { strret.clear(); char tmp[64] = { 0 }; if (node.IsString()) { strret = node.GetString(); return 0; } else if (node.IsDouble()) { //sprintf_s(tmp,63,"%.2lf",node.GetDouble()); sprintf(tmp,node.GetDouble()); strret = tmp; return 0; } else if (node.IsNumber()) { sprintf(tmp,"%.0lf",node.GetDouble()); strret = tmp; return 0; } else if (node.IsFalse()) { strret = "false"; return 0; } else if (node.IsTrue()) { strret = "true"; return 0; } return -1; } /** * 查找Json数据中的值 * * @param node Json格式的Value * @param key 关键字 * @param value 值 * * @return -1表示失败,其他情况表示数组中第几个 */ static int getIndexWithJsonArray(rapidjson::Value &node,const char *key,const char *value) { int index = -1; if (node.IsArray()) { for (unsigned int i = 0; i < node.Size(); i++) { //const char* temp = node[i][key].GetString(); //LS_LOG("%s%s",value,temp); if (strcmp(value,node[i][key].GetString()) == 0) { index = i; break; } } } CCASSERT(index != -1,"not find value in array"); return index; } static void sortJsonArray(rapidjson::Value &node,bool isAsc = true) { CCASSERT(node.IsArray(),"is not array"); rapidjson::Value temp; int dir = isAsc ? 1 : -1; for (unsigned int j = 0; j < node.Size(); j++) for (unsigned int i = 0; i < node.Size() - 1; i++) { CCASSERT(node[i].HasMember(key),"not member"); CCASSERT(node[i][key].IsInt(),"not int"); if (dir * node[i][key].GetInt() > dir * node[i + 1][key].GetInt()) { temp = node[i]; node[i] = node[i + 1]; node[i + 1] = temp; } } }