rapidjson效率高,所以之前cocostudio里面解析用的jsoncpp也换成了rapidjson。

引擎又带有rapidjson库,所以不用单独去下载,直接就可以用。

这里主要写一下关于解析和存储的代码笔记。

1. 解析

#include "json/rapidjson.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h" #define FILE_Name "EmailInfo.txt" #define KEY_Root       "EmailList"
#define KEY_Id "id"
#define KEY_Type    "type"
#define KEY_Content "content"
#define KEY_BonusId "bonus_unitID"
#define KEY_BonusCount   "bonusCount"
#define KEY_IsRead "isAlreadyRead"
#define KEY_RecvTime   "recvTime" //将要解析的json格式
// {
// "EmailList":[
// {
// "id":1,
// "type":1,
// "content":"string",
// "bonus_unitID":99,
// "bonusCount":10,
// "isAlreadyRead":false,
// "recvTime":11111
// }
// ]
// } //解析函数
bool EmailHelper::initData()
{
string filePath = FileUtils::getInstance()->getWritablePath();
filePath += FILE_Name;
Data dat = FileUtils::getInstance()->getDataFromFile(filePath);
if (dat.getSize() <= )
{
// no file
CCLOG("%s ---> no file [%s]", __FUNCTION__, filePath.c_str());
return false;
}
char *buf = new char[dat.getSize()+];
strncpy(buf, (char*)dat.getBytes(), dat.getSize());
buf[dat.getSize()] = ; //重新分配内存就是为了这一步,否则直接用Data来解析会解析错误。
CCLOG("%s ---> %s\n", __FUNCTION__, buf); rapidjson::Document d;
d.Parse<>(buf);
delete [] buf;
if (d.HasParseError()) //解析错误
{
CCLOG("%s ---> GetParseError %s\n", __FUNCTION__, d.GetParseError());
return false;
}
else if (d.IsObject())
{
if (d.HasMember(KEY_Root) && d[KEY_Root].IsArray()/* && d[KEY_Root].Size() > 0*/)
{
const rapidjson::Value& arr = d[KEY_Root];
for (int i=,count=arr.Size(); i<count; ++i)
{
Email temp;
if (arr[i].HasMember(KEY_Id)) temp.id = arr[i][KEY_Id].GetUint();
if (arr[i].HasMember(KEY_Type)) temp.type = (Email::EmailType)arr[i][KEY_Type].GetUint();
if (arr[i].HasMember(KEY_Content)) temp.content = arr[i][KEY_Content].GetString();
if (arr[i].HasMember(KEY_BonusId)) temp.bonus_unitID = arr[i][KEY_BonusId].GetUint();
if (arr[i].HasMember(KEY_BonusCount)) temp.bonusCount = arr[i][KEY_BonusCount].GetUint();
if (arr[i].HasMember(KEY_IsRead)) temp.isAlreadyRead = arr[i][KEY_IsRead].GetBool();
if (arr[i].HasMember(KEY_RecvTime)) temp.recvTime = arr[i][KEY_RecvTime].GetInt();
m_vecEmailList.push_back(temp);
}
return true;
}
} return false;
}

2. 存储

bool EmailHelper::saveData()
{
rapidjson::Document document;
document.SetObject(); //Set this value as an empty object.
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
//value
rapidjson::Value emailArr(rapidjson::kArrayType);
for (int i=,count=m_vecEmailList.size(); i<count; ++i)
{
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember<unsigned int>(KEY_Id, m_vecEmailList[i].id, allocator);
object.AddMember<unsigned int>(KEY_Type, m_vecEmailList[i].type, allocator);
object.AddMember<const char*>(KEY_Content, m_vecEmailList[i].content.c_str(), allocator);
object.AddMember<unsigned int>(KEY_BonusId, m_vecEmailList[i].bonus_unitID, allocator);
object.AddMember<unsigned int>(KEY_BonusCount, m_vecEmailList[i].bonusCount, allocator);
object.AddMember<bool>(KEY_IsRead, m_vecEmailList[i].isAlreadyRead, allocator);
object.AddMember<int>(KEY_RecvTime, m_vecEmailList[i].recvTime, allocator);
emailArr.PushBack(object, allocator);
}
document.AddMember(KEY_Root, emailArr, allocator); rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer); string filePath = FileUtils::getInstance()->getWritablePath();
filePath += FILE_Name;
FILE* fp = fopen(FileUtils::getInstance()->fullPathForFilename(filePath).c_str(),"w+");//具有读写属性,写的时候如果文件存在,会被清空,从头开始写。
if (!fp)
{
CCASSERT(fp,"file open faild!");
return false;
}
fwrite(buffer.GetString(),sizeof(char),buffer.Size(),fp);
fclose(fp); CCLOG("%s %s ---> %s", __FUNCTION__, filePath.c_str(), buffer.GetString());
return true;
}

cocos2d-x3.x使用rapidjson的更多相关文章

  1. cocos2d_x_06_游戏_一个都不能死

    终于效果图: 环境版本号:cocos2d-x-3.3beta0 使用内置的物理引擎 游戏主场景 // // HeroScene.h // 01_cocos2d-x // // Created by b ...

  2. cocos2d-x3.0创建第一个jsb游戏

    第一步: 最新的cocos2d-x.下载地址https://github.com/cocos2d/cocos2d-x github上最新的引擎,值得注意的是官网上发布的引擎是稳定版.选择哪种就看个人喜 ...

  3. cocos-2d解决rapidjson的string参数转换

    解决方法 AddMember的传入的参数不是string, 所以会报错 本质就是把string类型转换成 参数的类型 username = "string"; rapidjson: ...

  4. cocos2dx 3.2 定义自己使用rapidjson阅读json数据

    一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...

  5. rapidjson常见使用示例

    目录 目录 1 1. 前言 2 2. Move语意 2 3. rapidjson::Document 2 4. 成员迭代器MemberIterator 3 5. 数组迭代器ValueIterator ...

  6. cocos2dx中的Rapidjson

    1 Json基础 JSON 概念和特点: JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式,类似 XML ...

  7. Rapidjson的简单使用示例

    很早就想用用Markdown了,一直没机会.今天就来试一下 先放个目录: Rapidjson的简单使用示例 rapidjson官方教程 本示例所用环境 示例代码与注释 rapidjson官方教程 如果 ...

  8. Cocos2d-X网络编程(5) 使用Rapidjson解析数据

    Json基础及28种c++解析库性能对比 JSON 概念和特点:     JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)     JSON ...

  9. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  10. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

随机推荐

  1. lighttpd为什么要accept多次呢

    在lighttpd网络模型里面我们可以看到以下代码 /* accept()s at most 100 connections directly * * we jump out after 100 to ...

  2. pomelo windows 环境

    1.先安装 Python; 通过Python 官网 http://www.python.org/getit/ 下载并安装最新版本. 然后将Python 的安装目录(如: C:\Program File ...

  3. 制作java可执行程序的方法

    命令行方法: 1. 创建Manifest.txt文件,内容: Main-Class: com.mkyong.awt.AwtExample 2.打包所有的class,包括Manifest.txt文件: ...

  4. notepad++汉字突然横过来了

    修改notepad++,汉字突然横过来了,如图, 百度了一下,原来是因为选择的字体"@微软雅黑"前面的@符号惹的祸,改成"微软雅黑"就没事了.

  5. HDU 4901 The Romantic Hero (计数DP)

    The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...

  6. 常用的Activex 控件

    1. Flash Player  ActiveX Control 6.0.47.0 与FLASH 6.0配套的浏览器端动画播放插件                  download.pchome.n ...

  7. react native 遇到的坑

    1.项目中新加入组件,应执行npm install命令 2.项目执行react-native run-android 报错,应进入android目录,执行gradlew.bat clean命令 3.L ...

  8. st_Alarm_GenAlarmDealTime

    USE [ChiefmesNew]GO/****** Object: StoredProcedure [dbo].[st_Alarm_GenAlarmDealTime] Script Date: 04 ...

  9. 数据库连接字符串ConnectionString 中的关键字值释义

    ConnectionString 类似于 OLE DB 连接字符串,但并不相同.与 OLE DB 或 ADO不同,如果“Persist Security Info ”值设置为false(默认值),则返 ...

  10. IE取消访问剪贴板设置

    1. 启动IE. 2. Internet选项. 3. “安全”选项卡. 4. 自定义级别. 5. “安全设置-Internet区域”->脚本->允许对剪贴板进行编程访问(启用)