项目地址:
https://github.com/nlohmann/json/tree/v3.9.1
下载include.zip,解压后使用single_include文件夹下nlohmann
将该文件夹添加至自己的include直接使用
#include方法2using json = nlohmann::json;
c++ 解析json
https://github.com/open-source-parsers/jsoncpp
下载release版本,源码编译
这里下载的是
解压后新建build文件夹,用cmake进行编译,选择x64,vs2017,用vs2017分别编译release和debug版本,注意区分x64和32,release和debug版本
编译后的目录结构
接下来挑选编译好的lib,dll和相应的头文件,此处以debug为例,自己配置相应的lib和include的目录
- jsoncpp-1.9.4buildlibDebugjsoncpp.lib 拷贝到lib文件夹
- jsoncpp-1.9.4includejson 拷贝到自己的include文件夹
- jsoncpp-1.9.4buildbinDebug 下的dll文件拷贝到自己项目生成的exe同级目录下
完整测试代码,代码只是说明库文件可以用,具体也没什么含义
#include "json/json.h" #include#include using namespace std; #pragma comment(lib,"jsoncpp.lib") int main() { const char* str = "{"name":"xiaoming","age":18}"; int nRoleDd = 0; string strOccupation = "abc"; string strCamp = "cs"; Json::CharReaderBuilder b; Json::CharReader* reader(b.newCharReader()); Json::Value root; JSONCPP_STRING errs; bool ok = reader->parse(str, str + std::strlen(str), &root, &errs); if (ok&&errs.size() == 0) { std::string upload_id = root["uploadid"].asString(); // 访问节点,upload_id = "UP000000" int code = root["code"].asInt(); // 访问节点,code = 100 } delete reader; cout << "role_id is: " << nRoleDd << endl; cout << "occupation is: " << strOccupation << endl; cout << "camp is: " << strCamp << endl; return 0; }
结果显示:



