这里给出一个demo,很简单,就是点击右下角那个菜单会解析1.txt的那个json文件,需要把demo复制到cocos的project文件夹下面运行
主要代码:
void HelloWorld::menuCloseCallback(CCObject* pSender) { unsigned long size=0; std::string patch = CCFileUtils::sharedFileUtils()->fullPathForFilename("1.txt"); Json::Value jsonvalue; Json::Reader reader; unsigned char* data = CCFileUtils::sharedFileUtils()->getFileData(patch.c_str(), "r", &size);//读取文件内容 std::string document=(char*)data;//强行转换为string if(reader.parse(document, jsonvalue)) { std::cout<<jsonvalue["name"]; } }
Demo下载:
Github下载:https://github.com/DamonHu/jsoncppdemo
GitOsc下载:http://git.oschina.net/DamonHoo/jsoncppdemo
运行环境:xcode6.0 cocos2d-x2.2.6
版权属于:东哥笔记 - DongGe.org
本文链接:https://dongge.org/blog/85.html
本文采用知识共享署名4.0 国际许可协议进行许可。转载或大段使用必须添加本文链接,否则您将构成侵权!