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. 多级联动导航栏(top导航)

    http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/ This is a multi-lev ...

  2. nodejs + socket.io + redis 新手上路

    最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...

  3. nova --debug image-list

    nova --debug image-list DEBUG (session:) REQ: curl -g -i -X GET http://liberty-aio:35357/v3 -H " ...

  4. Spark SQL概念学习系列之Spark SQL 架构分析(四)

    Spark SQL 与传统 DBMS 的查询优化器 + 执行器的架构较为类似,只不过其执行器是在分布式环境中实现,并采用的 Spark 作为执行引擎. Spark SQL 的查询优化是Catalyst ...

  5. Struts2的运行流程以及关键拦截器介绍

    Struts2的运行流程 1.ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方 ...

  6. NSLog说明

    %@ 对象 %d,%i 整型 (%i的老写法) %hd 短整型 %ld , %lld 长整型 %u 无符整型 %f 浮点型和double型 %0.2f 精度浮点数,只保留两位小数 %x,%X 二进制整 ...

  7. jQuery基础学习1

    标准HTML文件引入jQuery库方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  8. HDU 3265 Posters (线段树+扫描线)(面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...

  9. Webstorm2016激活码[ 转]

    转至:http://blog.csdn.net/tingwode2014_/article/details/51063657 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  10. [html]html常用代码

    上传文件表单属性 enctype="multipart/form-data" 单选(是否选中) checked="checked" 下拉列表(是否选中) sel ...