当前位置:网站首页>nlohmann json
nlohmann json
2022-07-07 09:26:00 【heater404】
1 brief introduction
github Address :nlohmann/json: JSON for Modern C++ (github.com)
2 Integrate
stay release Download json.hpp File can . Then load the file into the project for use .

3 Use
here , We mainly introduce the serialization and deserialization of custom objects as json Format , So we need to define a data structure .
#pragma once
using namespace std;
#include <string>
#include "../json/json.hpp"
using namespace nlohmann;
namespace models
{
typedef enum
{
male = 0,
female = 1,
} gender;
typedef struct
{
string name;
uint8_t age;
gender gen;
} person;
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, age, gen);
}
Pay attention to the last key , With it, you can serialize the structure of custom data .
3.1 Serialize the custom object to json In file
models::person p{"zhuangjuan", 28, models::gender::female};// Define a person object
nlohmann::json j = p;// take person Object to json object
ofstream fout("person.json",ios::out);
fout<<j.dump();// take json Object to string Type written to file
fout.close();
3.2 take json File deserialization to object
string msg;
ifstream fin("person.json", ios::in);
fin >> msg;
fin.close();// take json File read out
json j = nlohmann::json::parse(msg);// Convert string to json object
models::person p = j.get<models::person>();// take json Object to person object
4 Be careful
4.1 Serialize the object to json When ,json By default, the structure in is sorted alphabetically , How can I customize sorting , To be further studied
4.2 By default ,json Comments are not allowed in structures . If you have to comment, you need to use “//”. And then the json String conversion to json When the object , You need to pay attention to a parameter . But I tried and I don't know why I still can't ....
json j = nlohmann::json::parse(msg,nullptr,false,true);
边栏推荐
- 如何使用clipboard.js库实现复制剪切功能
- Locust performance test 3 (high concurrency, parameter correlation, assembly point)
- 网易云微信小程序
- Jenkins+ant+jmeter use
- Unity uses mesh to realize real-time point cloud (I)
- Postman interface test (I. installation and use)
- Mysql database index study notes
- VSCode+mingw64+cmake
- Mysql:select ... for update
- Huawei hcip datacom core_ 03day
猜你喜欢

NATAPP内网穿透

What is the use of PMP certificate?

超十万字_超详细SSM整合实践_手动实现权限管理
![[SVN] what is SVN? How do you use it?](/img/45/a7df8989f18f0a6185582389398d1a.png)
[SVN] what is SVN? How do you use it?

Information Security Experiment 2: using x-scanner scanning tool

Jemter operation

Using JWT to realize login function

十二、排序

Locust performance test 5 (analysis)

Selenium mouse sliding operation event
随机推荐
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
网易云微信小程序
Jemter operation
Pytest installation (command line installation)
四、机器学习基础
VSCode+mingw64+cmake
Several stages of PMP preparation study
Summary of PMP learning materials
数据库多表关联查询问题
What is MD5
liunx命令
Why is access to the external network prohibited for internal services of the company?
Locust performance test 5 (analysis)
信息安全实验一:DES加密算法的实现
PMP Exam Preparation experience, seek common ground while reserving differences, and successfully pass the exam
How to speed up video playback in browser
Postman interface test (II. Set global variables \ sets)
MySql数据库-事务-学习笔记
Huawei hcip datacom core_ 03day
sqlplus乱码问题,求解答