https://github.com/nlohmann/json 全篇介绍
代码中:
一、添加头文件和名称空间(首先要引入json作用域,json提供了cin,cout的输入输出流的操作符):
#include <nlohmann/json.hpp>
using json = nlohmann::json;
二、将得到的json字符串转换成标准格式:UTF-8(世界通用的语言编码)。 ps:假设我们得到的json字符串是GBK格式
#include <boost/locale.hpp>
std::string j_string = boost::locale::conv::between(json字符串, "UTF-8", "GBK");
注释:
UTF-8:世界通用的语言编码
GBK:汉字国标扩展码
用 j_string 来接收json字符串最终转化的字符串,从GBK型的字符串转化成UTF-8型字符串
三、显示解析标准json字符串,生成对象j3。(j3是json库生成的自定义对象,j_string是标准json字符串)
auto j3 = json :: parse(j_string);
注释:
auto 对象 = json :: parse(标准json字符串);
四:遍历对象j3,用for循环
注释:对象是一个数组,里边含有多个对象,遍历数组,才能取对象的值,it相当于指针,记着这么用就行
for (json::iterator it = j3.begin(); it != j3.end(); ++it)
{
//取对象的值
}