当前位置:网站首页>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);
边栏推荐
- Postman interface test (I. installation and use)
- JWT certification used in DRF
- Interface test API case, data and interface separation
- Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
- Mysql database index study notes
- Data association between two interfaces of postman
- stm32和电机开发(从单机版到网络化)
- 战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
- NVIC interrupt priority management
- 2021 year end summary
猜你喜欢

Cesium load vector data

Jenkins automated email

Run can start normally, and debug doesn't start or report an error, which seems to be stuck

Summary of PMP learning materials

PMP certificate preparation experience sharing

浏览器中如何让视频倍速播放

Implementation of corner badge of Youmeng message push

Zen - batch import test cases
![Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]](/img/33/9fde4bce4866b988dd2393a665a48c.jpg)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]

12、 Sort
随机推荐
Jemter operation
Jenkins automated email
Unity shader (basic concept)
esp8266使用TF卡并读写数据(基于arduino)
Serializer & modelserializer of DRF serialization and deserialization
Idea development environment installation
Locust performance test 2 (interface request)
網易雲微信小程序
Regularly modify the system time of the computer
How to pass the PMP Exam in a short time?
How long does the PMP usually need to prepare for the exam in advance?
Redis common commands
What are the suggestions for PMP candidates?
PMP examination experience sharing
NVIC interrupt priority management
Unity uses mesh to realize real-time point cloud (II)
(3/8) method parameters of improper use of enumeration (2)
Summary of PMP learning materials
Do you have any certificates with high gold content?
flex弹性布局