加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:27:54 所属栏目:百科 来源:网络整理
导读:--转自http://blog.csdn.net/ironyoung/article/details/41599161?utm_source=tuicoolutm_medium=referral Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。 rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码

--转自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
  1. #include"CCStdC.h"
  2. #include"cocos2d.h"
  3. #include"json/document.h"
  4. #include"json/writer.h"
  5. #include"json/stringbuffer.h"
  6. usingnamespacerapidjson;
  7. USING_NS_CC;
  8. intmain()
  9. {
  10. //***生成json文件,存储在getWritablePath文件夹下***
  11. rapidjson::Documentwritedoc;
  12. writedoc.SetObject();
  13. rapidjson::Document::AllocatorType&allocator=writedoc.GetAllocator();
  14. rapidjson::Valuearray(rapidjson::kArrayType);
  15. rapidjson::Valueobject(rapidjson::kObjectType);
  16. //jsonobject格式添加“名称/值”对
  17. object.AddMember("inttag",1,allocator);
  18. "doubletag",1.0,allocator);
  19. "booltag",true,0);background-color:inherit;">"hellotag","helloworld",0);background-color:inherit;">//json加入数组
  20. array.PushBack(object,allocator);
  21. //jsonobject格式添加“名称/值”对
  22. writedoc.AddMember("json",0);background-color:inherit;">"jsonstring",0);background-color:inherit;">"array",array,0);background-color:inherit;">StringBufferbuffer;
  23. rapidjson::Writer<StringBuffer>writer(buffer);
  24. writedoc.Accept(writer);
  25. autopath=FileUtils::getInstance()->getWritablePath();
  26. path.append("myhero.json");
  27. FILE*file=fopen(path.c_str(),0);background-color:inherit;">"wb");
  28. if(file)
  29. {
  30. fputs(buffer.GetString(),file);
  31. fclose(file);
  32. }
  33. CCLOG("%s",buffer.GetString());
  34. return0;
  35. }


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

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

读取JSON文件并显示

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

[cpp] view plain copy
  1. #include"CCStdC.h"
  2. #include"cocos2d.h"
  3. #include"json/document.h"
  4. #include"json/writer.h"
  5. #include"json/stringbuffer.h"
  6. namespacerapidjson;
  7. USING_NS_CC;
  8. intmain()
  9. {
  10. autopath=FileUtils::getInstance()->getWritablePath();
  11. "myhero.json");
  12. //***读取json文件***
  13. rapidjson::Documentreaddoc;
  14. boolbRet=false;
  15. ssize_tsize=0;
  16. std::stringload_str;
  17. //getFileData如果不指定,读取根目录是Resource文件夹
  18. unsignedchar*titlech=FileUtils::getInstance()->getFileData(path,0);background-color:inherit;">"r",&size);
  19. load_str=std::string((constchar*)titlech,size);
  20. //load_str=cocos2d::FileUtils::getInstance()->getStringFromFile("..myhero.json");
  21. readdoc.Parse<0>(load_str.c_str());
  22. if(readdoc.HasParseError())
  23. {
  24. "GetParseError%sn",readdoc.GetParseError());
  25. }
  26. if(!readdoc.IsObject())
  27. return0;
  28. rapidjson::Value&_json=readdoc["json"];
  29. char*ch=_json.GetString();
  30. cocos2d::log(ch);
  31. cocos2d::log(_json.GetString());
  32. rapidjson::Value&_array=readdoc["array"];
  33. if(_array.IsArray())
  34. "test");
  35. for(inti=0;i<_array.Capacity();i++)
  36. //CCLOG("%d",i);
  37. rapidjson::Value&arraydoc=_array[i];
  38. if(arraydoc.HasMember("inttag"))
  39. int_inttag=arraydoc["inttag"].GetInt();
  40. "%d",_inttag);
  41. "doubletag"))
  42. double_doubletag=arraydoc["doubletag"].GetDouble();
  43. "%lf",_doubletag);
  44. }
  45. "booltag"))
  46. bool_booltag=arraydoc["booltag"].GetBool();
  47. "hellotag"))
  48. char*_hellotag=arraydoc["hellotag"].GetString();
  49. }
CCLOG 的最终显示为:

json string json string test 1 1.000000 1 helloworld

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读