且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows10 VS2017 C++ Json解析(使用jsoncpp库)

更新时间:2022-06-20 02:15:37

版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85259297

1.项目必须是win32
2.生成的lib_json.lib放到工程目录下
3.incldue的头文件放到工程目录,然后设置工程->属性->配置属性->vc++目录->包含目录
4.工程->属性->配置属性->c/c+±>代码生成->运行库,设置运行库为多线程调试 (/MTd)

首先从github下载最新版jsoncpp,0.10.7
https://github.com/open-source-parsers/jsoncpp/releases
在makefile目录的msvc2010打开工程进行编译,将库文件makefiles\msvc2010\Debug\lib_json.lib添加到工程目录,如上步骤2,然后将include目录拷贝到工程目录,按照上边步骤3设置,然后如上步骤4调整设置
解析字符串json,写码:

#include "pch.h"
#include <iostream>
#include <json.h>

#pragma comment(lib, "lib_json.lib")

using namespace std;

int main()
{
	const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";

	Json::Reader reader;
	Json::Value root;
	if (reader.parse(str, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素  
	{
		std::string upload_id = root["uploadid"].asString();  // 访问节点,upload_id = "UP000000"  
		int code = root["code"].asInt();    // 访问节点,code = 100 
		cout << "upload_id: " << upload_id << endl;
		cout << "code: " << code << endl;
	}
}

Windows10 VS2017 C++ Json解析(使用jsoncpp库)

参考文章:
http://www.cnblogs.com/liaocheng/p/4243731.html
https://blog.csdn.net/shufac/article/details/52710100