当前位置:网站首页>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);
边栏推荐
- Difference between interface iterator and iteratable
- Leetcode question brushing record (array) combination sum, combination sum II
- C language pointer (exercises)
- 战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
- liunx命令
- 【SVN】SVN是什么?怎么使用?
- Postman interface test (I. installation and use)
- Information Security Experiment 4: implementation of IP packet monitoring program
- Mysql database lock learning notes
- Cesium load vector data
猜你喜欢
浏览器中如何让视频倍速播放
Variable parameter of variable length function
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
NATAPP内网穿透
C language pointer (exercises)
Jmeters use
Colorbar of using vertexehelper to customize controls (II)
Unittest simple project
Jenkins+ant+jmeter use
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
随机推荐
How to use Arthas to view class variable values
Mysql数据库-锁-学习笔记
scrapy爬虫mysql,Django等
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
Pycharm create a new file and add author information
PMP Exam details after the release of the new exam outline
Unity shader (learn more about vertex fragment shaders)
Unity shader (data type in cghlsl)
Information Security Experiment 4: implementation of IP packet monitoring program
PMP certificate preparation experience sharing
Why is access to the external network prohibited for internal services of the company?
Binary tree high frequency question type
Entity of cesium data visualization (Part 1)
STM32 clock system
Confitest of fixture py
Regular matching starts with XXX and ends with XXX
Postman setting environment variables
The use of recycling ideas
Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
12、 Sort