您的位置:首页 > 编程语言 > C语言/C++

初学C++ 使用json对象----------rapidjson

2016-11-28 22:14 369 查看
RapidJSON是一个只有头文件的C++ JSON库,只需要拷贝“include/rapidjson”目录到工程包含路径下即可使用。

rapidjson库下载网地址:https://github.com/miloyip/rapidjson

rapidjson优点:

1.简单,易操作,RapidJSON只用包含起头文件即可使用;

2.更重要的是避免的采用jsoncpp生成静态链接库后,若要调用webservice会与clr产生的错误:

“/MTd”和“/clr”命令行选项不兼容

RapidJSON的使用

1.解压文件后,拷贝include\rapidjson文件夹到项目路径下。

2.引入rapidjson库头文件

#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using namespace rapidjson;//这个可能要手动输入,小助手可能看不到这个


3、使用

const char* str = "{\"filename\":\"wenjianming\",\"number\":22,\"result\":\"1\",\"a\":{\"b\":1}}";//注意这个格式,\"是转义"

Document doc;
// 在DOM中解析json字符串
doc.Parse(str);

// 读取json数据
const Value& filename= doc["filename"];
const char* s=filename.getString();


ps:尚未验证以下内容

RapidJSON项目的构建需要使用一个工具,就是premake

premake下载地址:http://industriousone.com/premake/download
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json c++