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" : "*****"
}
很簡單吧,那快開始你的編程之旅吧!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章