很早就想用用Markdown了,一直没机会。今天就来试一下
先放个目录:
- Rapidjson的简单使用示例
- rapidjson官方教程
- 本示例所用环境
- 示例代码与注释
rapidjson官方教程
如果要想深入学习rapidjson工具,官方文档肯定是必须看一看的。官方教程里面的讲解才是最详细,最权威的了。
本示例所用环境
示例代码与注释
说明:我是直接使用原版引擎创建了新的cocos2dx工程,然后略微修改了HelloWorldScene.cpp中的代码。为了方便,使用rapidjson生成json串、保存json串到文件、从文件读取json串、使用rapidjson解析json串的过程,全部写到了initSelf()函数中。
本身很反感blog全篇粘代码的方式(关键贴的代码谁都看不懂,一行注释没有),但是这部分代码示例没有什么可说的,所以在重点部分写了注释。
HelloWorldScene.h文件内容
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
private:
void initSelf();
};
HelloWorldScene.cpp文件内容
include "HelloWorldScene.h"
include "json/rapidjson.h"
include "json/document.h"
include "json/filestream.h"
include "json/stringbuffer.h"
include "json/writer.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
else {
this->initSelf();
true;
}
}
void HelloWorld::initSelf()
{
rapidjson::Document jsonDoc;
rapidjson::Document::AllocatorType &allocator = jsonDoc.GetAllocator();
jsonDoc.SetObject();
jsonDoc.AddMember("name","qfl",allocator);
jsonDoc.AddMember("age",20,allocator);
rapidjson::Value letterArray(rapidjson::kArrayType);
letterArray.PushBack("a",allocator);
letterArray.PushBack("b",97); background:transparent">"c",allocator);
jsonDoc.AddMember("letter",letterArray,128); background:transparent">//添加数组
rapidjson::Value locationObj(rapidjson::kObjectType);
locationObj.AddMember("province",97); background:transparent">"fujian",allocator);
locationObj.AddMember("city",97); background:transparent">"xiamen",97); background:transparent">"number",112); background:transparent">16,97); background:transparent">"location",locationObj,allocator);
rapidjson::Value bookArray//生成一个Array类型的元素,用来存放Object
rapidjson::Value book1(rapidjson::kObjectType);
book1.AddMember("book1",allocator);
book1.AddMember("isbn",97); background:transparent">"123",allocator);
bookArray.PushBack(book1,allocator);
rapidjson::Value book2//生成book2
book2.AddMember("book2",allocator);
book2.AddMember("456",allocator);
bookArray.PushBack(book2,128); background:transparent">//添加到数组
jsonDoc.AddMember("book",bookArray,allocator);
,true,128); background:transparent">//添加bool类型值
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
jsonDoc.Accept(writer);
std::string strJson = buffer.GetString();
log("-----生成的Json:n%s",strJson.c_str());
string strPath = FileUtils::getInstance()->getWritablePath() + "JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(),97); background:transparent">"w");
if (myFile) {
fputs(buffer.GetString(),myFile);
fclose(myFile);
}
"-----读取Json内容:");
rapidjson::Document newDoc;
myFile = fopen(strPath.c_str(),97); background:transparent">"r");
if (myFile) {
rapidjson::FileStream inputStream(myFile);
newDoc.ParseStream<0>(inputStream);
fclose(myFile);
}
if (newDoc.HasParseError()) {
"Json Parse error:%d",newDoc.GetParseError());
}
else {
if (newDoc.HasMember("name")) {
"name:%s",newDoc["name"].GetString());
}
else {}
"age")) {
"age:%d",97); background:transparent">"age"].GetInt());
}
"letter")) {
rapidjson::Value letter;
letter = newDoc["letter"];
if (letter.IsArray() && !letter.Empty()) {
for (rapidjson::SizeType i = 0; i < letter.Size(); i++) {
"letter:%s",letter[i].GetString());
}
}
else {}
}
"location")) {
rapidjson::Value location;
location = newDoc["location"];
if (location.IsObject()) {
if (location.HasMember("province")) {
"location:province:%s",location["province"].GetString());
}
else {}
"city")) {
"location:city:%s",97); background:transparent">"city"].GetString());
}
"number")) {
"location:number:%d",97); background:transparent">"number"].GetInt());
}
else {}
}
else {}
"book")) {
rapidjson::Value book;
book = newDoc["book"];
if (book.IsArray() && !book.Empty()) {
rapidjson::Value tempBook;
0; i < book.Size(); i++) {
tempBook = book[i];
if (tempBook.IsObject()) {
if (tempBook.HasMember("name") && tempBook.HasMember("isbn")) {
"book:%d:name:%s,isbn:%s",i,tempBook["name"].GetString(),97); background:transparent">"isbn"].GetString());
}
else {}
}
else {}
}
}
"healthy")) {
if (newDoc["healthy"].GetBool()) {
"healthy:true");
}
else {
"healthy:false");
}
}
else {}
}
} (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|