1. 如何使用?

2. 常用方法

2.1 创建json对象

2.1.1 使用cin,cout输入输出流

2.1.2 提供根据键直接生成键值对的方法

2.1.3 json::array json::object

2.1.4 几个区别

2.2 序列化

2.2.1 标准输出自动序列化

2.2.2 使用dump()函数

2.3 反序列化

2.3.1 从标准输入反序列化

2.3.2 通过附加_json到字符串文字来创建对象实现反序列化

2.3.3 使用json::parse()函数

2.3.4 从迭代器范围读取

2.3 与STL适应

2.4 STl与json转换

2.5 json最重要的还是设计config文件

最后

JSON(Java Object Notation) 是一种轻量级的数据交换格式。它基于ECMA的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、Java、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。

今天介绍的json版本是在c++下使用的,是一个开源项目。

GitHub开源项的地址:https://github.com/nlohmann/json

json for modern c++是一个德国大牛nlohmann写的,该版本的json有以下特点:

1.直观的语法。

2.整个代码由一个头文件组成json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便。

3.使用c++11标准编写。

4.使用json 像使用STL容器一样。

5.STL和json容器之间可以相互转换。

1. 如何使用?

将github上的src文件夹里的json.hpp头文件下载保存到当前目录中。

在代码中包含json.hpp头文件并引入json作用域

#include "json.hpp"

using json = nlohmann::json;

1

2

2. 常用方法

2.1 创建json对象

2.1.1 使用cin,cout输入输出流

json提供了cin,cout的输入输出流的操作符。但需要注意的是,cin要有ctr + D结束输入。cout会自动把json转换为string。

#include

#include "json.hpp"

using json = nlohmann::json;

using std::cout;

using std::endl;

using std::cin;

int main(int argc, const char * argv[]) {

json temp;

cin >> temp;

cout << temp << endl;

return 0;

}

/*

输入:

{

"pi": 3.141,

"happy": true,

"name": "Niels",

"nothing": null,

"answer": {

"everything": 42

},

"list": [1, 0, 2],

"object": {

"currency": "USD",

"value": 42.99

}

}

{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}

Program ended with exit code: 0

*/

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

2.1.2 提供根据键直接生成键值对的方法

#include

#include "json.hpp"

using json = nlohmann::json;

using std::cout;

using std::endl;

using std::cin;

int main(int argc, const char * argv[]) {

// create an empty structure (null)

json j;

// add a number that is stored as double (note the implicit conversion of j to an object)

j["pi"] = 3.141;

// add a Boolean that is stored as bool

j["happy"] = true;

// add a string that is stored as std::string

j["name"] = "Niels";

// add another null object by passing nullptr

j["nothing"] = nullptr;

// add an object inside the object

j["answer"]["everything"] = 42;

// add an array that is stored as std::vector (using an initializer list)

j["list"] = { 1, 0, 2 };

// add another object (using an initializer list of pairs)

j["object"] = { {"currency", "USD"}, {"value", 42.99} };

// instead, you could also write (which looks very similar to the JSON above)

json j2 = {

{"pi", 3.141},

{"happy", true},

{"name", "Niels"},

{"nothing", nullptr},

{"answer", {

{"everything", 42}

}},

{"list", {1, 0, 2}},

{"object", {

{"currency", "USD"},

{"value", 42.99}

}}

};

cout << j << endl;

cout << endl;

cout << j2 << endl;

return 0;

}

/*

{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}

{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}

Program ended with exit code: 0

*/

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

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

2.1.3 json::array json::object

在所有上述情况下,你不需要“告诉”编译器要使用哪个JSON值。如果你想明确或表达一些边缘的情况下,可以使用json::array,json::object。

#include

#include "json.hpp"

using json = nlohmann::json;

using std::cout;

using std::endl;

using std::cin;

int main(int argc, const char * argv[]) {

// a way to express the empty array []

json empty_array_explicit = json::array();

// ways to express the empty object {}

json empty_object_implicit = json({});

json empty_object_explicit = json::object();

// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]

json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };

for (auto object : array_not_object) {

cout << object << endl;

}

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

2.1.4 几个区别

array是一个数组,可以用数字直接下标访问。

json array = {

"yan",12,"ze",13

};

cout << array[0] << endl;

1

2

3

4

array是一个数组里面放了一个数组。认为”yan”,12是数组里面的两个元素。

json array = {

{"yan",12},

"ze",13

};

cout << array[0][0] << endl;

1

2

3

4

5

array是数组里面有一个数组(里面包含了一个键值对)。

json array = {

{{"yan",12}},

"ze",13

};

cout << array[0]["yan"] << endl;

1

2

3

4

5

array里面含有两个键值对。

json array = {

{"yan",12},

{"ze",13}

};

cout << array["yan"] << endl;

1

2

3

4

5

array里面含有两个数组,数组里面分别有一个键值对。

json array = {

{

{"yan",12}

},

{

{"ze",13}

}

};

cout << array[0]["yan"] << endl;

1

2

3

4

5

6

7

8

9

(注意区分{}的位置来判断对象的性质)。

如果实在判断不出来,可以用C++11的for语句,看每一个输出结果的符号来判断每一个对象的性质。

2.2 序列化

将json对象序列化,成为字符串

2.2.1 标准输出自动序列化

json j

std :: cout << j;

// setw操纵器被重载以设置漂亮打印的缩进

std :: cout << std :: setw( 4)<< j << std :: endl;

1

2

3

4

2.2.2 使用dump()函数

//显式转换为string

std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}

//序列化与漂亮的打印

//传入空格的数量缩进

std::cout << j.dump(4) << std::endl;

// 输出:

//{

// "happy": true,

// "pi": 3.141

// }

1

2

3

4

5

6

7

8

9

10

11

2.3 反序列化

将数据流转化为json对象

2.3.1 从标准输入反序列化

json j

std :: cin >> j;

1

2

2.3.2 通过附加_json到字符串文字来创建对象实现反序列化

//从字符串文字创建对象

json j = " { \" happy \":true,\" pi \":3.141} " _json;

//或者原始字符串文字

auto j2 = R"(

{

"happy":true,

"pi":3.141

}

)" _json;

1

2

3

4

5

6

7

8

9

10

请注意,没有附加_json后缀,传递的字符串文字不会被解析,而只是用作JSON字符串值。也就是说,json j = "{ “happy”: true, “pi”: 3.141 }“只存储字符串”{ “happy”: true, “pi”: 3.141 }"而不是解析实际的对象。

2.3.3 使用json::parse()函数

//明确解析

auto j3 = json::parse(" { \" happy \":true,\" pi \":3.141} ");

1

2

2.3.4 从迭代器范围读取

您还可以从迭代器范围读取JSON; 也就是说,可以从其内容存储为连续字节序列的迭代器访问的任何容器,例如std::vector

std :: vector < uint8_t > v = { ' t ',' r ',' u ',' e ' };

json j = json :: parse(v.begin(),v.end());

//或

std :: vector < uint8_t > v = { ' t ',' r ',' u ',' e ' };

json j = json :: parse(v);

1

2

3

4

5

6

2.3 与STL适应

json提供了许多和STL类似的操作方法:

#include

#include "json.hpp"

using json = nlohmann::json;

using std::cout;

using std::endl;

using std::cin;

int main(int argc, const char * argv[]) {

// create an array using push_back

json j;

j.push_back("foo");

j.push_back(1);

j.push_back(true);

// iterate the array

for (json::iterator it = j.begin(); it != j.end(); ++it) {

std::cout << *it << '\n';

}

// range-based for

for (auto element : j) {

std::cout << element << '\n';

}

// getter/setter

const std::string tmp = j[0];

j[1] = 42;

bool foo = j.at(2);

// other stuff

j.size(); // 3 entries

j.empty(); // false

j.type(); // json::value_t::array

j.clear(); // the array is empty again

// convenience type checkers

j.is_null();

j.is_boolean();

j.is_number();

j.is_object();

j.is_array();

j.is_string();

// comparison

cout << (j == "[\"foo\", 1, true]"_json) << endl; // true

// create an object

json o;

o["foo"] = 23;

o["bar"] = false;

o["baz"] = 3.141;

// special iterator member functions for objects

for (json::iterator it = o.begin(); it != o.end(); ++it) {

std::cout << it.key() << " : " << it.value() << "\n";

}

// find an entry

if (o.find("foo") != o.end()) {

// there is an entry with key "foo"

cout << *o.find("foo") << endl;

}

// or simpler using count()

int foo_present = o.count("foo"); // 1

int fob_present = o.count("fob"); // 0

cout << foo_present << endl;

cout << fob_present << endl;

// delete an entry

o.erase("foo");

cout << (o.find("foo") == o.end()) << endl;

return 0;

}

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

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

2.4 STl与json转换

允许把STL与json相转换。

#include

#include

#include

#include

#include

#include

#include "json.hpp"

using json = nlohmann::json;

using std::cout;

using std::endl;

using std::cin;

int main(int argc, const char * argv[]) {

std::vector c_vector {1, 2, 3, 4};

json j_vec(c_vector);

// [1, 2, 3, 4]

std::deque c_deque {1.2, 2.3, 3.4, 5.6};

json j_deque(c_deque);

// [1.2, 2.3, 3.4, 5.6]

std::list c_list {true, true, false, true};

json j_list(c_list);

// [true, true, false, true]

std::forward_list c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};

json j_flist(c_flist);

// [12345678909876, 23456789098765, 34567890987654, 45678909876543]

std::array c_array {{1, 2, 3, 4}};

json j_array(c_array);

// [1, 2, 3, 4]

std::set<:string> c_set {"one", "two", "three", "four", "one"};

json j_set(c_set); // only one entry for "one" is used

// ["four", "one", "three", "two"]

std::unordered_set<:string> c_uset {"one", "two", "three", "four", "one"};

json j_uset(c_uset); // only one entry for "one" is used

// maybe ["two", "three", "four", "one"]

std::multiset<:string> c_mset {"one", "two", "one", "four"};

json j_mset(c_mset); // only one entry for "one" is used

// maybe ["one", "two", "four"]

std::unordered_multiset<:string> c_umset {"one", "two", "one", "four"};

json j_umset(c_umset); // both entries for "one" are used

// maybe ["one", "two", "one", "four"]

return 0;

}

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

41

42

43

44

45

46

47

48

49

50

对于关联容器,json直接把map中的键值对转化为json的object。

std::map<:string int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };

json j_map(c_map);

// {"one": 1, "three": 3, "two": 2 }

std::unordered_map c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} };

json j_umap(c_umap);

// {"one": 1.2, "two": 2.3, "three": 3.4}

std::multimap<:string bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };

json j_mmap(c_mmap); // only one entry for key "three" is used

// maybe {"one": true, "two": true, "three": true}

std::unordered_multimap<:string bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };

json j_ummap(c_ummap); // only one entry for key "three" is used

// maybe {"one": true, "two": true, "three": true}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

json还提供隐式类型转换。

/// strings

std::string s1 = "Hello, world!";

json js = s1;

std::string s2 = js;

// Booleans

bool b1 = true;

json jb = b1;

bool b2 = jb;

// numbers

int i = 42;

json jn = i;

double f = jn;

// etc.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

也可以使用显式类型转换。

std::string vs = js.get<:string>();

bool vb = jb.get();

int vi = jn.get();

// etc.

1

2

3

4

2.5 json最重要的还是设计config文件

例如以下:

json question = {

{"question",{

{

{"id",0},

{"choice_type","single"},

{"grading",{

{"max_grade",10},

{"half_grade",5}

}},

{"standard_answer",{1}},

{"deion","this is a test for choice judger"},

{"choice",{

{

{"id",0},

{"deion","nope"}

},

{

{"id",1},

{"deion","B"}

},

{

{"id",2},

{"descrition","C"}

},

{

{"id",3},

{"deion","D"}

}

}

}

},

{

{"id",1},

{"choice_type","double"},

{"grading",{

{"max_grade",10},

{"half_grade",5}

}},

{"standard_answer",{1, 2}},

{"deion","this is a test for choice judger"},

{"choice",{

{

{"id",0},

{"deion","nope"}

},

{

{"id",1},

{"deion","B"}

},

{

{"id",2},

{"descrition","C"}

},

{

{"id",3},

{"deion","D"}

}

}

}

},

{

{"id",2},

{"choice_type","multi"},

{"grading",{

{"max_grade",10},

{"half_grade",5}

}},

{"standard_answer",{1,2}},

{"deion","this is a test for choice judger"},

{"choice",{

{

{"id",0},

{"deion","nope"}

},

{

{"id",1},

{"deion","B"}

},

{

{"id",2},

{"descrition","C"}

},

{

{"id",3},

{"deion","D"}

}

}

}

}

}

},

{"deion","this is choice questions!"}

};

json answer = {

{

{"question_id",1},

{"choice_id",{1}}

},

{

{"question_id",0},

{"choice_id",{1}}

},

{

{"question_id",2},

{"choice_id",{1,2,3}}

}

};

---------------------

作者:AI图哥

原文:https://blog.csdn.net/sinat_24143931/article/details/84974154

版权声明:本文为博主原创文章,转载请附上博文链接!

java multimap 序列化_C++ JSON库的使用相关推荐

  1. Java 关于序列化、Json和Gson详解

    文章目录 前言 序列化 什么是序列化 Java序列化 Java序列化.反序列化的基本用法 Java序列化的注意事项 JSON 和 Gson JSON概述 JSON语法规则 Gson Gson概述 Gs ...

  2. Java 几种常用 JSON 库性能比较

    作者:熊能 原文地址:www.xncoding.com/2018/01/09/java/jsons.html 本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能.每次都在网上看到别人说什 ...

  3. JSON用于多态Java对象序列化

    长期以来,JSON已成为客户端和服务器之间各种数据序列化的事实上的标准. 除其他外,它的优势是简单和易于阅读. 但是,简单起了一些限制,我今天要谈的其中一个限制是:存储和检索多态Java对象. 让我们 ...

  4. Java序列化——JDK序列化与Json序列化

    Java原生序列化 Java原生序列化我们可能会了解的比较多,Java类通过实现Serializable接口来实现该类对象的序列化,这个接口非常特殊,没有任何方法,只起标识作用.Java序列化保留了对 ...

  5. Java中如何使用JSON进行文件解析

    目录 一.什么是JSON 二.JSON的用途 三.如何应用JSON 一.什么是JSON JSON是一种轻量级的数据交换格式.它基于ECMAScript的一个子集,采用完全独立于编程语言的文本格式来存储 ...

  6. JAVA中几种常用JSON库性能比较

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 作者:飞污熊 xncoding.com/2018/01/09/java/jsons.html 本 ...

  7. Java中常用的4个Json库,哪个性能更牛逼?

    来源:http://u6.gg/sDMab 前言 每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库.但是百闻不如一见,只有自己亲手测试过的才是最值得相信的,本篇通过JMH来测试一下Ja ...

  8. Java—基于Fastjson的JSON串序列化和反序列化模板总结

    关注微信公众号:CodingTechWork,一起学习进步. 介绍 模板需求说明   开发中经常遇到前端传递过来的JSON串的转换,后端需要解析成对象,有解析成List的,也有解析成Map的.   我 ...

  9. 【java】序列化:ProtoBuf 与 JSON 的比较

    1.概述 转载:序列化:ProtoBuf 与 JSON 的比较! 介绍 ProtoBuf 是google团队开发的用于高效存储和读取结构化数据的工具.什么是结构化数据呢,正如字面上表达的,就是带有一定 ...

最新文章

  1. 【重磅】旷视提出MegDetV2:目标检测/实例分割新系统
  2. python画代码-python画樱花树代码 具体代码介绍
  3. 痛苦的老师开心的我们
  4. 最长公共子序列LCS[C++题解]
  5. 作《互联网时代的软件革命--SaaS架构设计》上市了
  6. Matlab | 用Matlab写一首歌送给女朋友——程序员必备撩妹技能(Matlab源码)
  7. 使用Fiddler对android应用抓包 专题
  8. 如何针对数据进行分析
  9. Springboot小区物业管理系统毕业设计源码051745
  10. java 期刊杂志参考_参考文献可以引用整本期刊杂志吗?格式怎么写?
  11. c语言中0x13,int 0x13 常用功能详解
  12. 进入网页就自动弹出层
  13. 未来,你会反感虚拟现实沉浸式广告吗?
  14. Java web系统打包成exe安装文件
  15. Combined attach
  16. ClickHouse中文官方文档
  17. 黑盒测试方法——边界值分析法
  18. 看看美国人怎么做SEO
  19. 详解Windows系统中如何释放C盘空间(转)
  20. 6s手机为什么不显示4g网络连接服务器,苹果iPhone6S 设置4G网络的方法

热门文章

  1. linux 驱动入门 魏清,Linux下的SPI总线驱动(三)
  2. Mysql数据库查询当前操作的数据库名
  3. JavaScript 所有数据类型
  4. js 对象数组常用操作 我用到的
  5. 1 微信公众号开发 服务器配置 有什么用
  6. arm linux 开机电路_ARM Linux启动过程分析
  7. bootstrp-table 获取checkbox选中行的数据id
  8. linux就业技术指导,学linux前景怎么样
  9. scp会覆盖同名文件吗_你会Hypermesh一键式完成几何文件到求解文件的输出吗?
  10. HDU 1284 钱币兑换问题 (动态规划 背包方案数)