在cocos2d-x引入了rapidjson,它处理速度比其他的json库快,反正不管了,我们这边只是学习下如何使用。rapidjson官方网址: https://code.google.com/p/rapidjson/wiki/UserGuide,上面有wiki有部分说明文档,可以看下。

github仓库地址:https://github.com/miloyip/rapidjson

下面我们讲讲rapidjson读写文件。

直接贴代码:

TestJson.h

 #ifndef _TEST_JSON_H_
#define _TEST_JSON_H_ #include "json/document.h"
using namespace std;
class TestJson
{
public:
TestJson(void);
~TestJson(void);
//读取文件
void readFile( const char * fileName );
//添加字符串成员
void addStrMenber(const char *key,const char *value);
//是否存在成员
bool hasMenber(const char *key);
//删除成员
void removeMenber(const char *key);
//写入文件
bool writeFile( const char * fileName ); private:
rapidjson::Document _jsonDocument;
};
#endif

TestJson.cpp

 #include "TestJson.h"
#include <stdio.h>
#include "json/filestream.h"
#include "json/stringbuffer.h"
#include "json/writer.h"
TestJson::TestJson(void)
{
} TestJson::~TestJson(void)
{
} void TestJson::readFile( const char * fileName )
{
if (fileName == nullptr) return;
FILE * pFile = fopen (fileName , "r");
if(pFile){
//读取文件进行解析成json
rapidjson::FileStream inputStream(pFile);
_jsonDocument.ParseStream<>(inputStream);
fclose(pFile);
}
if(!_jsonDocument.IsObject()){
_jsonDocument.SetObject();
}
} void TestJson::addStrMenber(const char *key,const char *value)
{
rapidjson::Value strValue(rapidjson::kStringType);
strValue.SetString(value,_jsonDocument.GetAllocator());
_jsonDocument.AddMember(key,strValue,_jsonDocument.GetAllocator());
} bool TestJson::hasMenber(const char *key)
{
if (_jsonDocument.HasMember(key)) {
return true;
}
return false;
} void TestJson::removeMenber(const char *key)
{
if (_jsonDocument.HasMember(key)) {
_jsonDocument.RemoveMember(key);
}
} bool TestJson::writeFile( const char * fileName )
{
if (fileName == nullptr) return false;
//转为字符串格式
rapidjson::StringBuffer buffer;
rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
_jsonDocument.Accept(writer);
const char* str = buffer.GetString();
FILE * pFile = fopen (fileName , "w");
if (!pFile) return false;
fwrite(str,sizeof(char),strlen(str),pFile);
fclose(pFile);
return true;
}

rapidjson 使用教程的更多相关文章

  1. rapidjson使用总结

    Reference:  https://blog.csdn.net/elloop/article/details/49908689 rapidjson简介 rapidjson是腾讯的开源json解析框 ...

  2. Rapidjson的简单使用示例

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

  3. RapidJSON v1.1.0 发布简介

    时隔 15.6 个月,终于发布了一个新版本 v1.1.0. 新版本除了包含了这些日子收集到的无数的小改进及 bug fixes,也有一些新功能.本文尝试从使用者的角度,简单介绍一下这些功能和沿由. P ...

  4. RapidJSON 1.0 正式版发布,C++的JSON开发包

    分享 <关于我> 分享  [中文纪录片]互联网时代                 http://pan.baidu.com/s/1qWkJfcS 分享 <HTML开发MacOSAp ...

  5. Navicat 激活教程2021(Linux)

    Navicat 激活教程2021(Linux) 目录 背景 环境 激活 清理 使用 背景 Navicat 是香港卓软数字科技有限公司生产的一系列 MySQL.MariaDB.MongoDB.Oracl ...

  6. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  7. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  8. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  9. Angular2入门系列教程4-服务

    上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...

随机推荐

  1. php 四种基础算法 ---- 快速排序法

    4.快速排序法 代码: function quick_sort($arr) {    //先判断是否需要继续进行    $length = count($arr);    if($length < ...

  2. C语言版的16进制与字符串互转函数

    http://www.cnblogs.com/nio-nio/p/3309367.html /* // C prototype : void StrToHex(BYTE *pbDest, BYTE * ...

  3. Lucene4.X 高级应用

    Lucene 简介以及使用 Lucene, 一个基于 Java 的开源的全文搜索工具包,可以方便的嵌入到各种应用系统中,实现针对应用的全文索引以及检索功能.目前是 Apache  jakarta 项目 ...

  4. VIJOS P1647 不差钱 SBT

    [描述] 同学们一起看了小品<不差钱>,LX神突发奇想,想刁难一下十八居士,他让十八居士模拟一下点菜的过程. [输入格式] 输入第一行为一个数price,表示价钱大于price的菜赵本山都 ...

  5. hdu_5691_Sitting in Line(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...

  6. MVC3 Razor 根据不同页面使用不同Layout

    _ViewStart.cshtml运行于每一Page前, 所以通常在这里先设置Layout   下面代码为特定的controller指定Index,Edit,Create的模板 即Index对应  _ ...

  7. 分析UIWindow

    转载自:http://www.cnblogs.com/YouXianMing/p/3811741.html The UIWindow class defines an object known as ...

  8. Web爬去的C#请求发送

    public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...

  9. 关联容器(map):支持高效查找的容器,一种键值对的集合。

    #include <iostream> #include <string> #include <map> #include <vector> using ...

  10. Kubernetes 1.4 部署

    k8s 1.4 新版本部署 测试环境: node-: 10.6.0.140 node-: 10.6.0.187 node-: 10.6.0.188 kubernetes 集群,包含 master 节点 ...