Json库

概述

  • JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
  • JSON 是轻量级的文本数据交换格式
  • JSON 独立于语言:JSON 使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态(PHP,JSP,.NET)编程语言都支持 JSON
  • JSON 具有自我描述性,更易理解

简而言之:

JSON就是可以帮助我们对数据进行序列化和反序列化。
JSON在C++上的库为jsoncpp

Jsoncpp在Ubunto 20.04上的安装

1
sudo apt install libjsoncpp-dev

Json库文件:

alt text

Json的动静态库:

alt text

带Json库的C++编译指令:

Jsoncpp的使用

编译选项

1
g++ -o $@ $^ -std=c++11 -ljsoncpp

头文件

1
#include <jsoncpp/json/json.h>

相关对象

Value对象:万能对象,里面可以存放各种类型的kv值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Json::Value
{
Value &operator=(const Value& other);
Value& operator[](const std::string& key);
Value& operator[](const char* key);
Value removeMember(const char* key); 移除元素
const Value& operator[](ArrayIndex index) const;
Value& append(const Value& value); 添加数组元素
ArrayIndex size() const; 获取数组元素个数
std::string asString() const; 转string
const char* asString() const; 转char*
int asInt() const; 转int
float asFloat() const; 转float
bool asBool() const; 转bool
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class JSON_API StreamWriterBuilder : public StreamWriter::Factory
创建一个StreamWriter对象

class Json::StreamValue
{
virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
对Json::Value中的数据进行序列化
}

class JSON_API CharReaderBuilder : public CharReader::Factory
创建一个CharReader对象

class Json::CharReader
{
virtual bool parse
(
char const* beginDoc,
char const* endDoc,
Value* root,
JSONCPP_STRING* errs
) = 0;
对Json::Value中的数据进行反序列化
}

序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <jsoncpp/json/json.h>
int main()
{
const char* name = "尹承乾";
int age = 19;
float score[] = {77.5, 88, 99.5};

Json::Value val;
val["name"] = name;
val["age"] = 19;
val["score"].append(score[0]);
val["score"].append(score[1]);
val["score"].append(score[2]);

先创建一个StreamWriter对象
Json::StreamWriterBuilder swb;

StreamWriter对象创建StreamWriter对象
unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

序列化
stringstream ss;
int ret = sw->write(val, &ss);
if(ret != 0)
{
cout << "write failed!" << endl;
return -1;
}

打印
cout << ss.str() << endl;
return 0;
}

alt text{: height=75%, width=75%}

反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <jsoncpp/json/json.h>

using namespace std;

bool unserialize(const string &str)
{
Json::Value val;

先创建一个CharReaderBuilder对象
Json::CharReaderBuilder crb;

CharReaderBuilder对象创建CharReader对象
unique_ptr<Json::CharReader> cr(crb.newCharReader());

反序列化
string err;
bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &val, &err);
if (ret == false)
{
cout << "parse failed!" << endl;
return false;
}

拆包打印
cout << val["name"].asString() << endl;
cout << val["age"].asInt() << endl;
int sz = val["score"].size();
for (int i = 0; i < sz; i++)
cout << val["score"][i] << endl;

return true;
}

int main()
{
string str = R"({"name":"尹承乾", "age":18, "score":[12.3, 43.5, 56.6]})";
unserialize(str);
return 0;
}

alt text{: height=75%, width=75%}