当前位置:网站首页>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);
边栏推荐
- 浏览器中如何让视频倍速播放
- STM32 and motor development (from stand-alone version to Networking)
- Leetcode question brushing record (array) combination sum, combination sum II
- What is MD5
- Pycharm importing third-party libraries
- Information Security Experiment 2: using x-scanner scanning tool
- Jenkins modifies the system time
- Loxodonframework quick start
- Pycharm create a new file and add author information
- Detailed learning notes of JVM memory structure (I)
猜你喜欢
PMP certificate preparation experience sharing
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
Jmeters use
flex弹性布局
Register address name mapping
Locust performance test 5 (analysis)
二叉树高频题型
Where is the answer? action config/Interceptor/class/servlet
Postman interface test (I. installation and use)
随机推荐
The use of recycling ideas
Colorbar of using vertexehelper to customize controls (II)
Locust performance test 4 (custom load Policy)
ComputeShader
Information Security Experiment 3: the use of PGP email encryption software
Regularly modify the system time of the computer
在EXCEL写VBA连接ORACLE并查询数据库中的内容
Integer or int? How to select data types for entity classes in ORM
Postman interface test (I. installation and use)
Leetcode刷题记录(数组)组合总和、组合总和 II
答案在哪里?action config/Interceptor/class/servlet
Information Security Experiment 1: implementation of DES encryption algorithm
Jmeters use
Unity shader (basic concept)
Add new item after the outbound delivery order of SAP mm sto document is created?
信息安全实验一:DES加密算法的实现
Selenium mouse sliding operation event
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
VSCode+mingw64
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]