大佬教程收集整理的这篇文章主要介绍了Cocos2d-x中Json(rapidjson)的扩展,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
容我直接上代码
/** * 保存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; } } }
以上是大佬教程为你收集整理的Cocos2d-x中Json(rapidjson)的扩展全部内容,希望文章能够帮你解决Cocos2d-x中Json(rapidjson)的扩展所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。