cocos2d-x文件读写、Json操作

文件读写操作

 //1.获取可操作的程序文件std::string path1 = FileUtils::getInstance()->getWritablePath()+"user.txt"; cclOG(path1.c_str());//C:/Users/laixiao/AppData/Local/Test3/ //2.写入数据到文件 std::string str0 = "laixiao赖肖";//会有中文乱码(cocos2d-x认utf-8编码;可是内容是gb2312编码) FileUtils::getInstance()->writeStringToFile(str0,path1); //3.文件读取一个字符串 std::string str1 = FileUtils::getInstance()->getStringFromFile(path1); cclOG(str1.c_str()); //一:反序列化过程 /* 使用#include "spine/Json.h" 新建f1.txt文件,json格式的内容,保存到Data/f1.txt [ {"id":10000,"name":"记得"},{"id":10001,"name":"用"},{"id":10002,"name":"utf-8无Bom格式"},{"id":10003,"name":"编码"},{"id":10004,"name":"保存到工程目录"},"name":"Resource文件夹res文件夹下"},] */ //直接读取工程目录下的文件 std::string str2 = FileUtils::getInstance()->getStringFromFile("Data/f1.txt"); cclOG(str2.c_str()); //@R_502_5246@为json Json* f1Json = Json_create(str2.c_str()); //获取Json数组的一个元素 Json* node = f1Json->child; while (node) { //获取id,转换为int类型 int id = Json_getItem(node,"id")->valueInt; //获取id,转换为String类型 std::string name = Json_getItem(node,"name")->valueString; cclOG(name.c_str()); node = node->next; } //二:序列化过程 std::string str3 = "{\"id\":1000,\"name\":\"laixiao\"}"; cclOG(str3.c_str());

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...