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不支持。
解决方案:
暂无,求教