cocos2dx 3.X 中 json 文件生成与读取

--转自 http://blog.csdn.net/ironyoung/article/details/41599161?utm_source=tuicool&utm_medium=referral

Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。

rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码库。项目 wiki 见这里。下面通过两个实例来深入了解它在 cocos2dx 中的用法。

注:CCLOG() 函数需要在 DEBUG 模式下才有作用。

生成JSON文件并保存

[cpp]  view plain  copy
 print ?
  1. #include "CCStdC.h"  
  2. #include "cocos2d.h"  
  3. #include "json/document.h"  
  4. #include "json/writer.h"  
  5. #include "json/stringbuffer.h"  
  6. using namespace  rapidjson;  
  7.   
  8. USING_NS_CC;  
  9.   
  10. int main()  
  11. {  
  12.     //*** 生成 json 文件,存储在 getWritablePath 文件夹下 ***  
  13.     rapidjson::Document writedoc;  
  14.     writedoc.SetObject();  
  15.     rapidjson::Document::AllocatorType& allocator = writedoc.GetAllocator();  
  16.     rapidjson::Value array(rapidjson::kArrayType);  
  17.     rapidjson::Value object(rapidjson::kObjectType);  
  18.       
  19.     // json object 格式添加 “名称/值” 对  
  20.     object.AddMember("inttag", 1, allocator);  
  21.     object.AddMember("doubletag", 1.0, allocator);  
  22.     object.AddMember("booltag"true, allocator);  
  23.     object.AddMember("hellotag""helloworld", allocator);  
  24.       
  25.     // json 加入数组  
  26.     array.PushBack(object, allocator);  
  27.       
  28.     // json object 格式添加 “名称/值” 对  
  29.     writedoc.AddMember("json""json string", allocator);  
  30.     writedoc.AddMember("array", array, allocator);  
  31.    
  32.     StringBuffer buffer;  
  33.     rapidjson::Writer<StringBuffer> writer(buffer);  
  34.     writedoc.Accept(writer);  
  35.   
  36.     auto path = FileUtils::getInstance()->getWritablePath();  
  37.     path.append("myhero.json");  
  38.     FILE* file = fopen(path.c_str(), "wb");  
  39.     if(file)  
  40.     {  
  41.         fputs(buffer.GetString(), file);  
  42.         fclose(file);  
  43.     }  
  44.     CCLOG("%s",buffer.GetString());  
  45.   
  46.     return 0;  
  47. }  


我是用 VS2012 编译的,最终生成的json文件位于 \proj.win32\Debug.win32 文件夹下。打开内容如下:

{"json":"json string","array":[{"inttag":1,"doubletag":1,"booltag":true,"hellotag":"helloworld"}]}

读取JSON文件并显示

rapidjson 需要根据原 json 格式单独编写解析方法,因此根据以上生成方法,解析方法应该为:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "CCStdC.h"  
  2. #include "cocos2d.h"  
  3. #include "json/document.h"  
  4. #include "json/writer.h"  
  5. #include "json/stringbuffer.h"  
  6. using namespace  rapidjson;  
  7.   
  8. USING_NS_CC;  
  9.   
  10. int main()  
  11. {  
  12.     auto path = FileUtils::getInstance()->getWritablePath();  
  13.     path.append("myhero.json");  
  14.   
  15.     //*** 读取 json 文件 ***  
  16.     rapidjson::Document readdoc;  
  17.     bool bRet = false;  
  18.     ssize_t size = 0;  
  19.     std::string load_str;  
  20.   
  21.     // getFileData 如果不指定,读取根目录是 Resource 文件夹  
  22.     unsigned char* titlech = FileUtils::getInstance()->getFileData(path, "r", &size);  
  23.     load_str = std::string((const char*)titlech,size);  
  24.   
  25.     //load_str = cocos2d::FileUtils::getInstance()->getStringFromFile("..\\myhero.json");  
  26.     readdoc.Parse<0>(load_str.c_str());     
  27.     if(readdoc.HasParseError())  
  28.     {  
  29.         CCLOG("GetParseError%s\n", readdoc.GetParseError());  
  30.     }  
  31.   
  32.     if(!readdoc.IsObject())  
  33.         return 0;  
  34.   
  35.     rapidjson::Value& _json = readdoc["json"];  
  36.     const char* ch = _json.GetString();  
  37.     cocos2d::log(ch);  
  38.     cocos2d::log(_json.GetString());  
  39.   
  40.     rapidjson::Value& _array = readdoc["array"];  
  41.     if(_array.IsArray())  
  42.     {  
  43.         CCLOG("test");  
  44.         for(int i=0; i<_array.Capacity(); i++)  
  45.         {  
  46.             //CCLOG("%d", i);  
  47.             rapidjson::Value& arraydoc = _array[i];  
  48.             if(arraydoc.HasMember("inttag"))  
  49.             {  
  50.                 int _inttag = arraydoc["inttag"].GetInt();  
  51.                 CCLOG("%d", _inttag);  
  52.             }  
  53.             if(arraydoc.HasMember("doubletag"))  
  54.             {  
  55.                 double _doubletag = arraydoc["doubletag"].GetDouble();  
  56.                 CCLOG("%lf", _doubletag);  
  57.             }  
  58.             if(arraydoc.HasMember("booltag"))  
  59.             {  
  60.                 bool _booltag = arraydoc["booltag"].GetBool();  
  61.                 CCLOG("%d", _booltag);  
  62.             }  
  63.             if(arraydoc.HasMember("hellotag"))  
  64.             {  
  65.                 const char* _hellotag = arraydoc["hellotag"].GetString();  
  66.                 CCLOG("%s", _hellotag);  
  67.             }  
  68.         }  
  69.     }  
  70.   
  71.     return 0;  
  72. }  
CCLOG 的最终显示为:

json string
json string
test
1
1.000000
1
helloworld


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值