最好用的json库,也许是JSON for modern C++ 的最佳实践!解决nlohmann json中文无法解析的问题!

使用 nlohmann 解析 json 文件 nlohmann 是德国工程师,以其名字为工程名的项目又被成为。 阅读详情

GitHub开源项的地址:https://github.com/nlohmann/json

json for modern c++是一个德国大牛nlohmann写的,该版本的json有以下特点:

1.直观的语法。
2.整个代码由一个头文件组成json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便。
3.使用c++11标准编写。
4.使用json 像使用STL容器一样。
5.STL和json容器之间可以相互转换。

如何使用?

只需要带一个文件即可。

#include <json.hpp>
//...
using json = nlohmann::json;

详细的使用在GitHub中已经有了,相当于一个使用手册,也推荐大家直接看它自带的。

链接: 使用手册.

注意事项

本人在使用的过程中,发现在反序列化的时候,遇到中文会出现bug。

正常使用

#include"json.hpp"
#include <iostream>0
using json = nlohmann::json;

int main()
{
    json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
    std::cout<<j["happy"]<<std::endl;
    return 0;
}

在这里插入图片描述

解析中文

#include"json.hpp"
#include <iostream>0
using json = nlohmann::json;

int main()
{
    json j = "{ \"happy\": \"中文\", \"pi\": 3.141 }"_json;
    std::cout<<j["happy"]<<std::endl;
    return 0;
}

在这里插入图片描述

原因定位及解决方案

原因为:
在windows下默认编码格式为GBK,nlohmann::json不支持。
在这里插入图片描述

解决方案:

暂无,求教

nlohmann::json从入门到精通 阅读详情

相关推荐

json C++开源库:JSON for Modern C++的几个小问题

JSON for Modern C++是nlohmann在GitHub上开源的读写json文件的C++库,使用者仅需包含一个json.hpp文件即可。 开源地址为:GitHub - nlohmann/json: JSON for Modern C++ 使用起来比较简单的了,首先从GitHub上将源码下载到本地,使用CMake编译源码(确保勾选了JSON_BuildTests) 在编译工程中找到unit-readme.cpp、unit-serialization.cpp、unit-deserializa

Charlie's 3644

httplib + nlohmann::json上传数据时中文乱码解决

(2)调用httplib::Response对象的set_content()方法时设置编码格式。(1)调用httplib::Response对象的set_header()方法来设置编码格式。(1)将数据先转为UTF-8格式。1.1 编码格式使用UTF-8。

weixin_45053845的博客 2197

Modern C++ JSON nlohmann::json 使用详解

Modern C++ json使用方法

琅嬛福地 2515

C++使用JSON——JSON for Modern C++

详解:https://blog.youkuaiyun.com/forrid/article/details/78867679 json for modern c++的使用:https://blog.youkuaiyun.com/fengxinlinux/article/details/71037244 JSON for Modern C++ 是德国大牛nlohmann写的: https://github.com/nl...

猫哆哩o0 3719

读写json中文ASCII乱码问题的解决方法

下面小编就为大家带来一篇读写json中文ASCII乱码问题的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

给新人的储能系统入门图解:从电芯到EMS,一张图看懂BESS里那些‘黑话’

本文通过视觉化拆解储能系统,从电芯到EMS详细解析BESS中的关键组件和行业术语。涵盖电芯参数、模组组装、PCS功能、BMS与EMS分工,以及SOC、DOD等关键运维参数,帮助新人快速理解储能系统的核心技术和应用场景。

weixin_42522964的博客 595

JSON for modern c++ / nlohmann

json for modern c++ / nlohmann 最近在使用这个c++实现的的json库,作者在github上提供了源码,是个开源的c++项目 GitHub开源项的地址:https://github.com/nlohmann/json JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JS...

Dominic_S的博客 4267

Vben Admin权限管理全解析:从动态菜单到按钮级控制(附代码示例)

本文全面解析Vben Admin框架的权限管理系统,从动态菜单生成到按钮级细粒度控制,提供完整的实现方案和代码示例。深入探讨基于角色-权限模型的架构设计,分享前后端协作的最佳实践,帮助开发者快速构建安全高效的企业级后台系统。

weixin_30919429的博客 487

JSON for Modern C++ 使用总结

最近需要用C++实现JSON的序列化与反序列化,在网上苦苦找寻,一开始相中了Jsoncpp,下载下来苦苦编译啊,设置是真心头疼,对于我这个小白来说是真麻烦呀。各种大神的各种经验完全不管用,最后碰巧看到了这个德国大神的巨作。就是我送给你们的玩具啦~~ 首先引用两个中文博客,因为我英文较差,看中文还比较轻松一些。所以从一开始接触这个库就是靠的这两个大神指引道路的。 废话不多说了,那两个博客...

勿以恶小而为之,勿以善小而不为 3085

内存学习(3):DRAM的基础存储结构(存储层级、读写过程,刷新与暂存)

DRAM,全称为 Dynamic Random Access Memory ,中文名是“动态随机存取存储器”。所谓“动态”是和“静态”相对应的,芯片世界里还有一种 SRAM 静态随机存取存储器的存在。笼统地说,DRAM 的结构比 SRAM 更简单,面积占用更小,适合制作大容量的存储芯片;而 SRAM 结构复杂一些,一般使用六个晶体管,面积消耗大,但是读写速度快,而且因为 SRAM 只用到晶体管,所以在工艺上和逻辑芯片相兼容,我们可以在逻辑芯片上直接集成 SRAM。

呆呆象呆呆的博客 2万+

想在瑞芯微RK3588上跑视频分类模型?避开3D卷积这个坑,试试这几种NPU友好的方案

本文探讨了在瑞芯微RK3588上部署视频分类模型时如何避开3D卷积的性能陷阱,提供了三种NPU友好的替代方案:MobileNetV2+GRU组合、Temporal Shift Module(TSM)和帧级池化。这些方案在保持较高准确率的同时,充分利用RKNPU的硬件加速能力,显著提升推理效率。文章还分享了从模型转换到部署优化的完整流程和实战经验。

weixin_28724319的博客 418

JSON for Modern C++ 库的介绍与使用示例代码

JSON for Modern C++ 使用 1、json对象的创建; 2、字符串、输入输出流、文件流的序列化与反序列化; 3、形如STL的操作方法来操作json对象 4、_json_pointer 的使用、patch与merge_patch的补丁操作 5、读取json文件并存放为结构体;将结构体数据处理后存放入json文件

墨1024 4198

保姆级教程:用YOLOv11和CRNN从零搭建一个车牌识别系统(附完整代码)

本文提供了一份详细的YOLOv11和CRNN实战指南,教你从零搭建高精度车牌识别系统。内容包括环境配置、数据集处理、模型训练与优化,以及系统集成与性能优化技巧,附完整代码实现。特别介绍了YOLOv11在车牌检测中的创新应用和CRNN在车牌识别中的关键技术。

weixin_28358083的博客 321

JSON for Modern C++结构体序列化:5种方式实现C++类与JSON互转(ADL与NLOHMANN_DEFINE_TYPE宏实战)

📦 **JSON for Modern C++**(nlohmann/json)是 C++ 社区最流行的 JSON 库,它把 JSON 变成了现代 C++ 的一等公民。本文带你一次性掌握**结构体序列化**的 5 种实战方式:从手写 **ADL 序列化函数**,到一行代码完成的 **NLOHMANN_DEFINE_TYPE 宏**,让 C++ 类与 JSON 字符串轻松互转,不再手写繁琐的转换代

gitblog_00977的博客 659

ML.NET在工业上位机中的深度应用:如何用C#实现10ms级预测推理?

模型要“够用就好”:工业场景的规律往往不复杂,别迷信深度学习,FastTree、线性模型足够应付80%的场景,且速度快3-5倍;预处理“能省就省”:离线做90%的特征工程,实时只处理动态变化的10%,这是突破10ms的关键;代码“贴近硬件”:用struct、数组池、SIMD等“非托管技巧”写C#,避免GC成为实时性瓶颈;测试“模拟极端”:在目标工控机上用100%负载(同时跑PLC通信、运动控制、视觉检测)测试推理延迟,确保极端情况下仍达标。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 743

【科研绘图系列】R语言绘制代谢物与临床表型相关性的森林图

本研究通过森林图展示了特定代谢物与三种临床比较之间的相关性分析结果。这种方法不仅直观地展示了每个代谢物的效应大小和不确定性,还允许我们比较不同代谢物在不同临床比较中的影响。森林图的结果表明,某些代谢物在特定临床比较中显示出显著的相关性,这可能为理解疾病的代谢机制提供了重要线索。

专注生信领域 566

json for modern c++,这是我用过的最好用的json库了

GitHub开源项的地址:https://github.com/nlohmann/json json for modern c++是一个德国大牛nlohmann写的,有以下特点: 1.直观的语法。 2.整个代码由一个头文件组成json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便。 3.使用c++11标准编写。 4.使用json 像使用STL容器一样。 5.STL和json容器之间可以相互转换。 还有很多其他的特性,不一一列举了。 如何使用? 将github上的src文件夹

哦,原来你也在这里。 2495

【keil5和STC-ISP】保姆级下载安装教程

点击下载中的产品下载。然后就会看到如下界面:可以看到,提供了针对不同芯片架构的四套独立开发工具。:你使用的51单片机,其核心就是。C51套件里的编译器是专门为这个架构优化的。:如果安装了MDK-Arm,你在创建新项目时将像“AT89C51”这样的51系列芯片型号,无法进行开发。进入如下界面:双击下载 → 双击运行安装 → 同意协议并下一步 → 使用默认安装路径 → 完成安装。

深耕CV 1654

nlohmann/json 完整指南:C++ 单头文件 JSON 解析与序列化库从集成到生产

当 C++ 服务需要读取配置文件或处理 HTTP 响应时,JSON 的解析与序列化往往是最先要解决的集成项。nlohmann/json 是一个 C++ 单头文件 JSON 库:包含 json.hpp 后即可创建、解析和输出 JSON 对象,无需改动构建系统。 ## 项目速览 nlohmann/json(JSON for Modern C++)是面向现代 C++ 的 JSON 解析与序列化库,核

gitblog_00150的博客 1027

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

NLOHMANN_JSON_VERSION_MAJOR / MINOR / PATCH 详解:JSON for Modern C++(nlohmann/json)版本宏的定义原理与实战用法

本篇文章围绕 JSON for Modern C++(nlohmann/json)三个内置版本宏 `NLOHMANN_JSON_VERSION_MAJOR`、`NLOHMANN_JSON_VERSION_MINOR`、`NLOHMANN_JSON_VERSION_PATCH`,结合仓库源码讲解它们的默认取值、底层定义位置、与版本检查、inline namespace 及 `meta()` 查询接口

gitblog_00077的博客 1179

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

nlohmann/json:C++ JSON 库 5 分钟上手与选型指南

nlohmann/json 是一个单头文件的 C++ JSON 库,面向需要解析、生成、转换 JSON 数据的 C++ 开发者。把一个 json.hpp 放进工程就能干活:读写配置、处理 API 响应、切换二进制格式,不引入构建脚本,也不带第三方依赖。 [![C++ JSON 库 nlohmann/json 官方标题图](https://raw.gitcode.com/GitHub_Trendi

gitblog_00138的博客 787

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

nlohmann/json:C++ 单头文件 JSON 库选型参考

写一个工具要读 JSON 配置文件,或做一个网关要解析 API 响应,C++ 项目里绕不开 JSON。nlohmann/json 用一个头文件完成解析、序列化与格式转换,零依赖、纯 C++11,把这块需求收敛到最小集成成本。 ## 30 秒认识项目 - 项目:nlohmann/json(JSON for Modern C++) - 版本:3.12.0(见根目录 [CMakeLists.txt]

gitblog_00893的博客 1009

nlohmann::basic_json::erase:JSON for Modern C++ 删除元素的五重载全景与源码级解析

在 JSON for Modern C++(nlohmann/json)中,`nlohmann::basic_json::erase` 是"修改值"(Modifying values)一组成员,用于删除对象成员、数组元素以及通过迭代器定位的任意 JSON 值。它提供 5 个重载:按迭代器删除单元素、按迭代器区间删除、按对象键删除(含 C++17 `std::string_view` 异构键版本)、

gitblog_00582的博客 1114

JSON for Modern C++ 比较运算详解:nlohmann::json 的 operator>= 规则、实现与 C++20 演进

> 本文依据仓库文档 [operator_ge.md](https://link.gitcode.com/i/f1704f3b25714b570f04350ae0814671) 撰写,面向需要在 C++ 中直接对 JSON 值执行"大于等于"语义比较的开发者,覆盖三个重载签名、以 `!(lhs < rhs)` 为核心的推导规则、`NaN` 与 `discarded` 的特殊处理,以及 C++20

gitblog_01045的博客 970

JSON for Modern C++ 命名空间宏全解:NLOHMANN_JSON_NAMESPACE_BEGIN / END 的用法与底层原理

本篇文章以 nlohmann/json(JSON for Modern C++)API 文档中关于 `NLOHMANN_JSON_NAMESPACE_BEGIN` 与 `NLOHMANN_JSON_NAMESPACE_END` 的说明为骨架,结合仓库内 [abi_macros.hpp](https://link.gitcode.com/i/750b3d2c272d168baaf9ce27ebeef

gitblog_00033的博客 1126
上一篇: 手把手教你用SetWindowsHookEx做一个键盘记录器
下一篇: Windows编程下的字符串格式及编码问题
CodeBowl
博客等级 码龄5年 7664粉丝 67原创
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值