营销型网站建设tfx88,渠道网络建设怎么写,wordpress如何把导航栏一直固定,绵阳企业品牌网站建设nlohmann/json是一个用于解析JSON的开源C库#xff0c;口碑一流#xff0c;无需额外安装其他第三方库#xff0c;还支持单个头文件模式#xff0c;使用起来非常方便直观。
1. 编译
从官网https://github.com/nlohmann/json的Release页面下载单个json.hpp即可直接使用库口碑一流无需额外安装其他第三方库还支持单个头文件模式使用起来非常方便直观。
1. 编译
从官网https://github.com/nlohmann/json的Release页面下载单个json.hpp即可直接使用无需单独编译。
2. 使用示例
下面以示例的方式罗列nlohmann/json库的基本使用方法。
2.1 生成JSON
方式1
int main()
{using json nlohmann::json;json j;j[pi] 3.141;j[happy] true;j[name] Niels;j[nothing] nullptr;j[answer][everything] 42;j[list] { 1, 0, 2 };j[object] { {currency, USD}, {value, 42.99} };// 转成字符串std::string strJSON j.dump(2); // 2个空格的缩进std::cout strJSON;return 0;
}输出如下
{answer: {everything: 42},happy: true,list: [1,0,2],name: Niels,nothing: null,object: {currency: USD,value: 42.99},pi: 3.141
}方式2
int main()
{using json nlohmann::json;json j {{pi, 3.141},{happy, true},{name, Niels},{nothing, nullptr},{answer, {{everything, 42}}},{list, {1, 0, 2}},{object, {{currency, USD}, {value, 42.99}}}};// 转成字符串std::string strJSON j.dump(2);std::cout strJSON;return 0;
}输出内容与方式1一样。
方式3
int main()
{using json nlohmann::json;json j;j[pi] 3.141;j[happy] true;j[name] Niels;j[nothing] nullptr;json j_answer;j_answer[everything] 42;j[answer] j_answer;json j_list json::array();j_list.push_back(1);j_list.push_back(0);j_list.push_back(2);j[list] j_list;json j_object;j_object[currency] USD;j_object[value] 42.99;j[object] j_object;// 转成字符串std::string strJSON j.dump(2);std::cout strJSON;return 0;
}输出内容与方式1一样。
2.2 解析JSON
int main()
{using json nlohmann::json;std::string strJSON u8R({answer: {everything: 42},happy: true,list: [1,0,2],name: Niels,nothing: null,object: {currency: USD,value: 42.99},pi: 3.141});auto jsonObj json::parse(strJSON);std::cout jsonObj[pi].getfloat() std::endl; // 3.141std::cout jsonObj[pi].getdouble() std::endl; // 3.141std::cout std::boolalpha jsonObj[happy].getbool() std::endl; // truestd::cout jsonObj[name].getstd::string() std::endl; // Nielsassert(jsonObj[nothing] nullptr);std::cout jsonObj[answer][everything].getint() std::endl; // 42std::cout jsonObj[list].size() std::endl; // 3std::cout jsonObj[list][0].getint() std::endl; // 1std::cout jsonObj[list][1].getint() std::endl; // 0std::cout jsonObj[list][2].getint() std::endl; // 2std::cout jsonObj[object][currency].getstd::string() std::endl; // USDstd::cout jsonObj[object][value].getfloat() std::endl; // 42.99// 依次输出:// 1// 0// 2for (json::iterator it jsonObj[list].begin(); it ! jsonObj[list].end(); it) {std::cout *it std::endl;}return 0;
}3. 异常处理
当解析和生成JSON出错时nlohmann/json会抛出异常因此在解析和生成JSON时需要进行异常捕获。
int main()
{using json nlohmann::json;std::string strJSON u8R({pi: 3.141});try {auto jsonObj json::parse(strJSON);std::cout jsonObj[ppp].getfloat() std::endl;}catch (std::exception e) {std::cout e.what() std::endl;}return 0;
}4. 判断成员是否存在
int main()
{using json nlohmann::json;std::string strJSON u8R({pi: 3.141});auto jsonObj json::parse(strJSON);std::cout std::boolalpha jsonObj.contains(pi) std::endl; // truestd::cout std::boolalpha jsonObj.contains(ppp) std::endl; // falsereturn 0;
}欢迎访问我的个人站点https://jiangxueqiao.com