当前位置:网站首页>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);
边栏推荐
- Connecting mobile phone with ADB
- Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
- Leetcode question brushing record (array) combination sum, combination sum II
- 超十万字_超详细SSM整合实践_手动实现权限管理
- C language pointer (exercises)
- The use of recycling ideas
- PMP experience learning and sharing process
- Windows starts redis service
- When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
- Record of structured interview
猜你喜欢

Locust performance test 3 (high concurrency, parameter correlation, assembly point)

E-commerce campaign Guide

Jemter operation

Yapi test plug-in -- cross request
![[cloud native] Devops (I): introduction to Devops and use of code tool](/img/e0/6152b3248ce19d0dbba3ac4845eb65.png)
[cloud native] Devops (I): introduction to Devops and use of code tool

STM32 clock system

MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查

flex弹性布局

Loxodonframework quick start

二叉树高频题型
随机推荐
2021 year end summary
数据建模中利用3σ剔除异常值进行数据清洗
Unity shader (basic concept)
Self awakening from a 30-year-old female programmer
CMD startup software passes in parameters with spaces
Netease cloud wechat applet
Some pit avoidance guidelines for using Huawei ECS
Pycharm create a new file and add author information
Mysql database index study notes
Locust performance test 5 (analysis)
Cesium load vector data
How can I apply for a PMP certificate?
C language pointer (special article)
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
在EXCEL写VBA连接ORACLE并查询数据库中的内容
华为HCIP-DATACOM-Core_03day
Integer or int? How to select data types for entity classes in ORM
JVM garbage collection detailed learning notes (II)
Huawei HCIP - datacom - Core 03 jours
Information Security Experiment 2: using x-scanner scanning tool