描述JSON串

如何使用jsoncpp提供的数据结构来存储如下JSON串?

// Configuration options
{
// Default encoding for text
"encoding" : "UTF-8", // Plug-ins loaded at start-up
"plug-ins" : [
"python",
"c++", // trailing comment
"ruby"
], // Tab indent size
// (multi-line comment)
"indent" : { /*embedded comment*/ "length" : 3, "use_space": true }
}

jsoncpp使用Json::Value对象来保存JSON串,Json::Value对象可以表示如下数据类型:

枚举类型 说明 翻译
nullValue 'null' value 不表示任何数据,空值
intValue signed integer value 表示有符号整数
uintValue unsigned integer value 表示无符号整数
realValue double value 表示浮点数
stringValue UTF-8 string value 表示utf8格式的字符串
booleanValue bool value 表示布尔数
arrayValue array value (ordered list) 表示数组,即JSON串中的[]
objectValue object value (collection of name/value pairs) 表示键值对,即JSON串中的{}
整个JSON串可以看做一个Json::ValueType::objectValue类型的Json::Value对象,我们将其命名为`root`,`root`包含了许多子Json::Value对象,例如:
"encoding"是Json::ValueType::stringValue类型的Json::Value对象
"plug-ins"是Json::ValueType::arrayValue类型的Json::Value对象
"indent"是Json::ValueType::objectValue类型的Json::Value对象

读取数据

在知道如何描述JSON串后,我们来看下Json::Value类提供了哪些方法来访问保存的数据:

// 检测保存的数据类型
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isInt64() const;
bool isUInt() const;
bool isUInt64() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const; // 基础数据类型的访问
const char* asCString() const;
JSONCPP_STRING asString() const;
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
float asFloat() const;
double asDouble() const;
bool asBool() const; // ValueType::arrayValue和ValueType::objectValue数据类型的访问,操作方式很类似C++的vector,可以使用数组风格或者迭代器风格来操作数据
ArrayIndex size() const;
Value& operator[](ArrayIndex index);
Value& operator[](int index);
const Value& operator[](ArrayIndex index) const;
const Value& operator[](int index) const;
Value get(ArrayIndex index, const Value& defaultValue) const;
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end(); // ValueType::objectValue数据类型的访问,操作方式很类似C++的map
Value& operator[](const char* key);
const Value& operator[](const char* key) const;
Value& operator[](const JSONCPP_STRING& key);
const Value& operator[](const JSONCPP_STRING& key) const;
Value& operator[](const StaticString& key);
Value& operator[](const CppTL::ConstString& key);
const Value& operator[](const CppTL::ConstString& key) const;
Value get(const char* key, const Value& defaultValue) const;
Value get(const char* begin, const char* end, const Value& defaultValue) const;
Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;

修改数据

知道如何获取Json::Value对象或其子对象后,我们来看下如何修改Json::Value保存的数据:

// 直接使用=赋值就可以了
Value& operator=(Value other); // 因为Json::Value已经实现了各种数据类型的构造函数
Value(ValueType type = nullValue);
Value(Int value);
Value(UInt value);
Value(Int64 value);
Value(UInt64 value);
Value(double value);
Value(const char* value);
Value(const char* begin, const char* end);
Value(const StaticString& value);
Value(const JSONCPP_STRING& value);
Value(const CppTL::ConstString& value);
Value(bool value);
Value(const Value& other);
Value(Value&& other);

解析JSON串

前面了解了Json::Value是如何存储JSON串,但是如何解析JSON串并生成Json::Value对象呢?

bool Json::Reader::parse(const std::string& document, // JSON串
Value& root, // 这就时JSON串生成的Json::Value对象
bool collectComments = true); // 允许在反序列化的时候保存注释,然后在序列化的时候写回注释 bool Json::Reader::parse(const char* beginDoc, // 除了传入整个JSON串,还可以只指定其中的某一段进行解析
const char* endDoc,
Value& root,
bool collectComments = true); bool Json::Reader::parse(JSONCPP_ISTREAM& is, // 当然还可以传入标准输入流,例如打开的文件
Value& root,
bool collectComments = true);

生成JSON串

现在我们知道怎么从JSON串中获取Json::Value对象,现在让我们来看下如何将一个Json::Value对象转换成JSON串:

// 所有从Writer派生出来的类都可以直接将Json::Value对象转换成JSON字符串,可使用的类有:FastWriter、StyledWriter
class JSON_API Writer {
public:
virtual ~Writer();
virtual JSONCPP_STRING write(const Value& root) = 0;
}; // 所有从StreamWriter派生出来的类都可以直接将Json::Value对象转换成JSON字符串到输出流中,可使用的类有:BuiltStyledStreamWriter,但是这个类无法直接访问,只能通过StreamWriterBuilder::newStreamWriter方法直接获取类实例。注:如果只想获取字符串不想写到流里面就Writer派生的类
class JSON_API StreamWriter {
protected:
JSONCPP_OSTREAM* sout_; // not owned; will not delete
public:
StreamWriter();
virtual ~StreamWriter();
virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0; class JSON_API Factory {
public:
virtual ~Factory();
virtual StreamWriter* newStreamWriter() const = 0;
}; // Factory
}; // StreamWriter // 所有从StyledStreamWriter派生出来的类都可以直接将Json::Value对象转换成JSON字符串到输出流中,感觉和BuiltStyledStreamWriter实现类似。注:如果只想获取字符串不想写到流里面就Writer派生的类
class JSON_API StyledStreamWriter {
public:
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
~StyledStreamWriter() {} public:
void write(JSONCPP_OSTREAM& out, const Value& root);
}

JSONCPP介绍的更多相关文章

  1. C++ Jsoncpp源代码编译与解析Json

    1.Json 数据表示方式介绍 这个可以看之前的一个文章里面有说明:Java解析(读取)Json数据 2.C++ Jsoncpp 2.1 Jsoncpp介绍 (1)JsonCpp主要包含三种类型的cl ...

  2. 介绍一个非常好用的跨平台C++开源框架:openFrameworks

    介绍一个非常好用的跨平台C++开源框架:openFrameworks 简介 首先需要说明的一点是: openFrameworks 设计的初衷不是为计算机专业人士准备的, 而是为艺术专业人士准备的, 就 ...

  3. JsonCpp简单使用

    作者:ilife JsonCpp简单使用 1.相关概念总结 (1)解析json的方法 Json::Value json;     //表示一个json格式的对象 Json::Reader reader ...

  4. C++解析JSON之JsonCPP

    一.JSON简介 JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读.编写.解析. JSON由两种基本结构构成: )"名称/值" ...

  5. JSON数据解析——jsoncpp的使用

    版权所有,转载请注明:http://blog.sina.com.cn/u/1978765352 由于工作中需要用到JSON数据,所以解析JSON数据就成了一个非常重要的工作内容. 其实用C++解析数据 ...

  6. C++的Json解析库:jsoncpp和boost

    C++的Json解析库:jsoncpp和boost - hzyong_c的专栏 - 博客频道 - CSDN.NET C++的Json解析库:jsoncpp和boost 分类: 网络编程 开源库 201 ...

  7. c++ - Create empty json array with jsoncpp - Stack Overflow

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     multiprocessing.pool c++ - Create empty json array wit ...

  8. json简介及JsonCpp用法

    [时间:2017-04] [状态:Open] [关键词:数据交换格式,json,jsoncpp,c++,json解析,OpenSource] json简介 本文仅仅是添加我个人对json格式的理解,更 ...

  9. C++通过jsoncpp类库读写JSON文件-json用法详解

    介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...

随机推荐

  1. CDH- 测试mr

    cdh的mr样例算法的jar包在 [zc.lee@ip---- hadoop-0.20-mapreduce]$ pwd /opt/cloudera/parcels/CDH--.cdh5./lib/ha ...

  2. phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建

    phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建 场景:当前有一张表:Ntime,因为表太大了,考虑要分表: Ntime2017 Nti ...

  3. unity3D实现多点触碰

    实现多点触碰是利用input这个类里面的方法实现的. 从edit-project settings-input就可以看到input能够得到的轴. 想要读取轴向可以使用Input.GetAxis方法获取 ...

  4. vc中播放mp3文件的方法小结

    一般播放MP3常见的有两种方法,一种是自己解码,另外一种用系统的库,比如MCI,当然如果可以用控件直接用个控件会更方便. 1.      使用mci #include <windows.h> ...

  5. luogu1353 Running

    dp[i][j]表示走i分钟疲劳值为j时的最远距离 然后搞一下就好啦 #include <iostream> #include <cstdio> #include <al ...

  6. Python的几种版本的不同实现

    Python自身作为一门编程语言,它有多种实现.这里的实现指的是符合Python语言规范的Python解释程序以及标准库等.这些实现虽然实现的是同一种语言,但是彼此之间,特别是与CPython之间还是 ...

  7. RPG游戏地牢设计的29个要点

    转自:http://www.gameres.com/491660.html Troy 是一名 RPG 开发者,以整理了一些自己开发地下城 RPG 的经验,开发者不妨参考一下: 1.地下城应该有个地方无 ...

  8. cocos2d-x 屏幕分辨率适配方法

    转自:http://blog.csdn.net/somestill/article/details/9950403 bool AppDelegate::applicationDidFinishLaun ...

  9. Linux系统中‘dmesg’命令处理故障和收集系统信息的7种用法

    转自:https://linux.cn/article-3587-1.html 'dmesg'命令显示linux内核的环形缓冲区信息,我们可以从中获得诸如系统架构.cpu.挂载的硬件,RAM等多个运行 ...

  10. css 雪碧图

    CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问 该页面时,载入的图片就不会像以前那样一幅一幅地 ...