C++之使用jsoncpp解析json数据

      说到http协议那就离不开json数据的解析了,那么在C++中如何对json数据进行解析呢?博主在这里使用jsoncpp来解析json数据。

        首先需要搭建jsoncpp的环境,这里我就不再详细介绍,给大家推荐一篇博客参考:博客地址。如果还没下载jsoncpp的读者,可以通过下面链接下载,下载链接,密码:9nl3。下面进入jsoncpp的使用说明:

  一、引入头文件

#include "json\json.h"
#include "json\config.h"

二、代码实现

这里还是以上一篇C++之Libcurl库的使用的代码为例,这篇博客里面介绍了如何使用libcurl库进行网络传输。这里我们给这篇博客的实例代码添加一些新东西。

bool CLibcurl::UserLand(const std::string number, const std::string password)
{
	std::string  strUrl = "http://127.0.0.1:5000/User/Login";
	CURL *curl;
	CURLM *multi_handle;
	std::stringstream getstream;
	std::string str_json;
	Json::Value value_json;
	Json::Reader reader_json;
	int still_running;
	bool IsTrue = false;

	struct curl_httppost *formpost = NULL;
	struct curl_httppost *lastptr = NULL;
	struct curl_slist	 *headers = NULL;
	static const char buf[] = "Expect:";
	curl_global_init(CURL_GLOBAL_ALL);
	curl_formadd(&formpost, &lastptr,
		CURLFORM_COPYNAME, "username",
		CURLFORM_COPYCONTENTS, number.c_str(),
		CURLFORM_END);
	curl_formadd(&formpost, &lastptr,
		CURLFORM_COPYNAME, "password",
		CURLFORM_COPYCONTENTS, password.c_str(),
		CURLFORM_END);
		
	curl = curl_easy_init();
	multi_handle = curl_multi_init();
	if (NULL == curl || NULL == multi_handle) {		
		IsTrue = false;
		goto exit;
	}			

	headers = curl_slist_append(headers, buf);
#ifdef _DEBUG
	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);	
	curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);//表单
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &getstream);

	curl_multi_add_handle(multi_handle, curl);

	while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running));
	while (still_running) {
			
		switch (Select(multi_handle)) {
		case -1:
			break;
		case 0:
			std::cout << "timeout" << std::endl;
		default:
			while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running));
			break;
		}
	}
        // json数据解析
	str_json = getstream.str();
	reader_json.parse(str_json.c_str(), value_json);
	int info = value_json["info"].asInt();
	IsTrue = (info == 1) ? true : false;
	if (IsTrue)
		MonitorManager::GetInstance()->SetUserAccount(number);

exit:	
	curl_global_cleanup();
	curl_multi_cleanup(multi_handle);
	curl_easy_cleanup(curl);
	curl_formfree(formpost);
	curl_slist_free_all(headers);

	return IsTrue;
}
相信看了上篇博客的读者很容易发现这里其实就多了几条代码,如果对于libcurl不懂的读者也请细读下上篇博客。下面我们来说说上面新增部分的代码。
Json::Value value_json;
Json::Reader reader_json;
这里的value_json博主把它理解成一种容器,用于存储json格式的数据,而reader_json可以理解为解析器,将获取的字符串解析成json格式并存入value_json中,那我们来看看下面的解析过程。
reader_json.parse(str_json.c_str(), value_json);
int info = value_json["info"].asInt();

str_json为string类型,即接收到的数据,这是后台返回的json数据为

{
    "info" : 1
}

所以此时我们直接使用value_json["info"]获取返回的状态码。当然json数据并不会都这么的简单,面对复杂的json数据需要解析时,希望读者们勇于去尝试解析它们,大同小异,相信你们都没问题的。这里主要讲的是对jsoncpp的使用,就不在此深究json数据的解析了。

下面我们来说说json数据的封装

void package_data()
{
    Json::Value root;
    Json::FastWriter writer;
    Json::Value person;
 
    person["name"] = "faith";
    person["password"] = "*****";
    root.append(person);
 
    string json_file = writer.write(root);
    // 将封装的数据打印出来
    cout << json_file << endl;
}

生成的数据为:

{
    "name" : "faith",
    "age" : "*****"
}
很简单吧,那快开始你的编程之旅吧!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章