--转自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文件并保存
- #include"CCStdC.h"
- #include"cocos2d.h"
- #include"json/document.h"
- #include"json/writer.h"
- #include"json/stringbuffer.h"
- usingnamespacerapidjson; @H_404_78@
- USING_NS_CC; @H_404_78@intmain() @H_404_78@{
- //***生成json文件,存储在getWritablePath文件夹下*** @H_404_78@rapidjson::Documentwritedoc;
- writedoc.SetObject(); @H_404_78@rapidjson::Document::AllocatorType&allocator=writedoc.GetAllocator();
- rapidjson::Valuearray(rapidjson::kArrayType); @H_404_78@rapidjson::Valueobject(rapidjson::kObjectType); @H_404_78@//jsonobject格式添加“名称/值”对
- object.AddMember("inttag",1,allocator); @H_404_78@"doubletag",1.0,allocator);
- "booltag",true,0);background-color:inherit;">"hellotag","helloworld",0);background-color:inherit;">//json加入数组
- array.PushBack(object,allocator); @H_404_78@//jsonobject格式添加“名称/值”对 @H_404_78@writedoc.AddMember("json",0);background-color:inherit;">"jsonstring",0);background-color:inherit;">"array",array,0);background-color:inherit;">StringBufferbuffer; @H_404_78@rapidjson::Writer<StringBuffer>writer(buffer);
- writedoc.Accept(writer); @H_404_78@autopath=FileUtils::getInstance()->getWritablePath(); @H_404_78@path.append("myhero.json");
- FILE*file=fopen(path.c_str(),0);background-color:inherit;">"wb");
- if(file)
- { @H_404_78@fputs(buffer.GetString(),file);
- fclose(file); @H_404_78@}
- CCLOG("%s",buffer.GetString());
- return0; @H_404_78@}
我是用 VS2012 编译的,最终生成的json文件位于 \proj.win32\Debug.win32 文件夹下。打开内容如下:
{"json":"json string","array":[{"inttag":1,"doubletag":1,"booltag":true,"hellotag":"helloworld"}]}
读取JSON文件并显示
rapidjson 需要根据原 json 格式单独编写解析方法,因此根据以上生成方法,解析方法应该为:
- #include"CCStdC.h"
- #include"cocos2d.h"
- #include"json/document.h"
- #include"json/writer.h"
- #include"json/stringbuffer.h"
- namespacerapidjson; @H_404_78@
- USING_NS_CC;
- intmain() @H_404_78@{
- autopath=FileUtils::getInstance()->getWritablePath(); @H_404_78@"myhero.json"); @H_404_78@//***读取json文件***
- rapidjson::Documentreaddoc;
- boolbRet=false;
- ssize_tsize=0; @H_404_78@std::stringload_str;
- //getFileData如果不指定,读取根目录是Resource文件夹
- unsignedchar*titlech=FileUtils::getInstance()->getFileData(path,0);background-color:inherit;">"r",&size); @H_404_78@load_str=std::string((constchar*)titlech,size);
- //load_str=cocos2d::FileUtils::getInstance()->getStringFromFile("..\\myhero.json");
- readdoc.Parse<0>(load_str.c_str());
- if(readdoc.HasParseError())
- { @H_404_78@"GetParseError%s\n",readdoc.GetParseError());
- }
- if(!readdoc.IsObject())
- return0;
- rapidjson::Value&_json=readdoc["json"];
- char*ch=_json.GetString(); @H_404_78@cocos2d::log(ch);
- cocos2d::log(_json.GetString()); @H_404_78@rapidjson::Value&_array=readdoc["array"];
- if(_array.IsArray())
- "test");
- for(inti=0;i<_array.Capacity();i++) @H_404_78@//CCLOG("%d",i); @H_404_78@rapidjson::Value&arraydoc=_array[i];
- if(arraydoc.HasMember("inttag"))
- int_inttag=arraydoc["inttag"].GetInt(); @H_404_78@"%d",_inttag);
- "doubletag"))
- double_doubletag=arraydoc["doubletag"].GetDouble();
- "%lf",_doubletag); @H_404_78@}
- "booltag"))
- bool_booltag=arraydoc["booltag"].GetBool();
- "hellotag"))
- char*_hellotag=arraydoc["hellotag"].GetString();
- 404_78@}
json string json string test 1 1.000000 1 helloworld