# Json **Repository Path**: hnSVN/Json_test ## Basic Information - **Project Name**: Json - **Description**: 简单的json读写使用 - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-11-10 - **Last Updated**: 2023-07-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Json介绍 json是一种轻量级的数据交换格式(也叫数据序列方式)。Json采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得Json成为理想的数据交换语言。易于人们阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。 # 一个优秀的Json三方库 JSON for Modern C++ 是一个由德国大牛 nlohmann 编写的在 C++ 下使用的 JSON 库。 ### 具有以下特点: - 这里是列表文本直观的语法 - 这里是列表文本整个代码由一个头文件组成json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便 - 这里是列表文本使用 C++11 标准编写 - 这里是列表文本使用 json 像使用 STL 容器一样 - 这里是列表文本STL 和 json容器之间可以相互转换 # 包含头文件 ``` #include "json.hpp" using json = nlohmann::json; ``` # Json数据序列化 ## 普通数据序列化 ``` // testjson.cpp // json序列化代码1 void func1() { json js; js["msg_type"] = 2; js["from"] = "zhang san"; js["to"] = "li si"; js["msg"] = "hellom what are you doing now?"; // 将json数据 => 序列化成json字符串 // 这样就可以在网络上发送 string sendBuf = js.dump(); cout << sendBuf << endl; } [root@Zhn testjson]# g++ testjson.cpp -o testjson [root@Zhn testjson]# ./testjson {"from":"zhang san","msg":"hellom what are you doing now?","msg_type":2,"to":"li si"} [root@Zhn testjson]# ``` ``` // json序列化代码2 void func2() { json js; // 添加数组 js["id"] = {1, 2, 3, 4, 5}; // 添加key-value js["name"] = "zhang san"; // 添加对象 js["msg"]["zhang san"] = "hello world"; js["msg"]["liu shuo"] = "hello china"; // 上面等同于下面这句一次性添加数组对象 js["msg"] = {{"zhang san", "hello world"}, {"liu shuo", "hello china"}}; cout << js << endl; } [root@Zhn testjson]# g++ testjson.cpp -o testjson [root@Zhn testjson]# ./testjson {"id":[1,2,3,4,5],"msg":{"liu shuo":"hello china","zhang san":"hello world"},"name":"zhang san"} [root@Zhn testjson]# ``` ## 容器序列化 ``` // json序列化代码3 void func3() { json js; vector vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); js["list"] = vec; // 直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; cout << js << endl; } [root@Zhn testjson]# g++ testjson.cpp -o testjson [root@Zhn testjson]# ./testjson {"list":[1,2,3],"path":[[1,"黄山"],[2,"华山"],[3,"泰山"]]} [root@Zhn testjson]# ``` # Json数据反序列化 ``` // json数据反序列化 void func4() { json js; // 普通序列化 js["name"] = "zhang san"; // 容器序列化 vector vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); js["list"] = vec; // 直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; // 将json数据序列化为json字符串 string sendBuf = js.dump(); cout << sendBuf << endl; /********* * 将字符串发送到网络中 *********/ // 模拟从网络接收到json字符串,通过json::parse函数把json字符串反序列化为json对象 json js2 = json::parse(sendBuf); // 直接取key-value string name = js2["name"]; cout << "name = " << name << endl; // 反序列化为vector容器 vector v = js2["list"]; for (int val : v) { cout << val << " "; } cout << endl; // 反序列化为map容器 map m2 = js2["path"]; for (auto p : m2) { cout << p.first << " " << p.second << endl; } return; } [root@Zhn testjson]# g++ testjson.cpp -o testjson [root@Zhn testjson]# ./testjson {"list":[1,2,3],"name":"zhang san","path":[[1,"黄山"],[2,"华山"],[3,"泰山"]]} name = zhang san 1 2 3 1 黄山 2 华山 3 泰山 [root@Zhn testjson]# ``` # 将Json数据写入文件 ``` // 将json数据写进文件中 void func5() { func4(); // 文件名称 string path = "test.json"; ofstream os; os.open(path, ios::out); if (!os.is_open()) { cout << "文件打开失败..." << endl; return; } // 设置空格 os << js.dump(4) << endl; return; } ``` ``` // test.json { "list": [ 1, 2, 3 ], "name": "zhang san", "path": [ [ 1, "黄山" ], [ 2, "华山" ], [ 3, "泰山" ] ] } ```