json简介及JsonCpp用法

时间:2023-01-12 22:32:32

[时间:2017-04] [状态:Open]

[关键词:数据交换格式,json,jsoncpp,c++,json解析,OpenSource]

json简介

本文仅仅是添加我个人对json格式的理解,更详细的格式建议参考官网的JSON介绍及标准文档。

json中仅支持两种结构:

  • name->value键值对(pair)的集合,一般称为对象(object)。
  • 值的有序表,一般称为数组(array)。

1. pair

先从键值对(pair)开始,一个pair的通常结构是:

string:value

键值之间的对应关系使用:表示,左边的为name,右边的为value。

一般key使用字符串,当然也可以使用数字,但是不推荐。

value的取值就比较随便,可以是任何任何json支持的类型(比如object,array,string,number,true/false,null等)。

2. object

object可以认为是多个pair的集合,官网上给出的示意图如下:

json简介及JsonCpp用法

其语法是以{作为object开始,以}作为object结束,不同的pair之间使用,分割。

需要说明的是object中的数据存储是无序的。

下面是一个比较典型的object构成

{
"name" : "tocy",
"age" : 1000
}

3. array

array是value的有序集合。先看官网上针对array结构的示意图:

json简介及JsonCpp用法

其语法是以[作为array起始,以]作为array结束,array元素之间使用,分割。

实际使用中建议在array中使用统一的类型,否则解析起来会麻烦点。

比如下面语法是合法的:

[{"name":"tocy"}, {"age":1000}, {"domain":"cn"}]

当然下面这种写法也是可以的,[1, "ts", true, {"key":45}]

至于其他的string和number支持的格式,建议参考json官方介绍。

JsonCpp的使用

json的官网上有很多关于json解析的工具,可以从中选择一个你喜欢的。

我主要在c++下使用json,我选择了JsonCpp,官网是:https://github.com/open-source-parsers/jsoncpp。

下载完源码之后为了使用方便,直接将源码嵌入到工程中,进入源码所在目录,先生成一个完整的头文件和cpp文件,命令如下:

python amalgamate.py

然后将dist文件夹拷贝到工程目录就可以使用了。(包含json/json.h、json/json-forwards.h、json.cpp)

注意:本部分用例代码可能为了简化期间,并未做错误异常判断,仅供参考。

示例1 从文件中读取json文件并解析

首先我们提供一个json文件,这里命名为"checkjson.json",其中数据如下:

{
"name" : "tocy",
"age" : 1000
}

这里面保存的是最简单的object,我们可以使用下面代码将其读入并解析:

void demo_simple()
{
ifstream ifs;
ifs.open("checkjson.json");
assert(ifs.is_open()); Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
cerr << "parse failed \n";
return;
} string name = root["name"].asString(); // 实际字段保存在这里
int age = root["age"].asInt(); // 这是整型,转化是指定类型
}

这里是简单的map访问,然后直接读取对应字段即可。

示例2 从内存中读取json数据(object)

我们在内存中定义一段json数据,然后解析,这次我们在json中添加内嵌array的object。代码如下:

void demo_parse_mem_object()
{
const char json_data[] =
"{\"name\" : \"Tocy\", \"salary\" : 100, \"msg\" : \"work hard\", \
\"files\" : [\"1.ts\", \"2.txt\"]}"; Json::Reader reader;
Json::Value root;
// reader将Json字符串解析到root,root将包含Json里所有子元素
if (!reader.parse(json_data, json_data + sizeof(json_data), root))
{
cerr << "json parse failed\n";
return;
} cout << "demo read from memory ---------\n";
string name = root["name"].asString();
int salary = root["salary"].asInt();
string msg = root["msg"].asString();
cout << "name: " << name << " salary: " << salary;
cout << " msg: " << msg << endl;
cout << "enter files: \n";
Json::Value files = root["files"]; // read array here
for (unsigned int i = 0; i < files.size(); ++i)
{
cout << files[i].asString() << " ";
}
cout << endl << endl;
}

示例3 从内存中解析json数据(array)

这次我们从提供一个以array封装的json数据,解析逻辑如下:

void demo_parse_mem_array()
{
const char json_data[] =
"[{\"name\" : \"Tocy\", \"salary\" : 100}, {\"name\" : \"Kit\", \"salary\" : 89}, \
\"a json note\"]"; Json::Reader reader;
Json::Value root;
// reader将Json字符串解析到root,root将包含Json里所有子元素
if (!reader.parse(json_data, json_data + sizeof(json_data), root))
{
cerr << "json parse failed\n";
return;
} cout << "demo read from memory using array---------\n";
unsigned int count = root.size() - 1;
for (unsigned int i = 0; i < count; ++i)
{
string name = root[i]["name"].asString();
int salary = root[i]["salary"].asInt();
cout << "name: " << name << " salary: " << salary << endl;
}
cout << "last msg: " << root[count].asString() << endl;
cout << endl << endl;
}

示例4 简单json数据封装

前面三个是关于json数据解析的例子,下面是关于json数据封装的例子。

首先我们生成示例1的数据,代码如下:

void demo_write_simple()
{
Json::Value root;
Json::FastWriter writer;
Json::Value person; person["name"] = "tocy";
person["age"] = 1000;
root.append(person); string json_file = writer.write(root); cout << "demo write json ==============\n";
cout << json_file << endl;
}

示例5 json封装-内嵌array的object

首先我们生成示例2的数据,代码如下:

void demo_write_object()
{
Json::Value root;
Json::FastWriter writer; root["name"] = "tocy";
root["salary"] = 100;
root["msg"] = "work hard";
Json::Value files;
files[0] = "1.ts";
files[1] = "2.txt";
root["files"] = files; string json_file = writer.write(root);
cout << "demo write json object ==============\n";
cout << json_file << endl;
}

示例6 json封装-内嵌object的array

首先我们生成示例3的数据,代码如下:

void demo_write_array()
{
Json::Value root;
Json::FastWriter writer; {
Json::Value person;
person["name"] = "Tocy";
person["salary"] = 100;
root[0] = person;
} {
Json::Value person;
person["name"] = "Kit";
person["salary"] = 89;
root[1] = person;
} root[2] = "a json note"; string json_file = writer.write(root);
cout << "demo write json ==============\n";
cout << json_file << endl;
}

小结

本文简单整理了json相关格式。同时针对JsonCpp的用法做简单总结,仅供参考。

本文相关源码,可以从我的git下载,位于JsonCppTutorial目录下。