詳解C++的JSON靜態鏈接庫JsonCpp的應用辦法。本站提示廣大學習愛好者:(詳解C++的JSON靜態鏈接庫JsonCpp的應用辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C++的JSON靜態鏈接庫JsonCpp的應用辦法正文
JsonCpp安排辦法:
在http://sourceforge.net/projects/jsoncpp/中下載最新版本的jsoncpp庫源碼。
以後將jsoncpp-src-版本號-tar.gz解壓出來,翻開makefiles中的jsoncpp.sln停止編譯,以後build文件夾下的vs71\debug\lib_json中會有一個.lib靜態鏈接庫。
JsonCpp重要包括三品種型的class:Value Reader Writer。
jsoncpp中一切對象、類名都在namespace json中,包括json.h便可
留意:Jsoncpp 的 Json::Writer 類是一個純虛類,其實不能直接應用。在此我們應用 Json::Writer 的子類:Json::FastWriter、Json::StyledWriter、Json::StyledStreamWriter。
反序列化Json(解析Json)
{ "name": "xiaoming″, "like": [ { "book": "json" }, { "food": "apple" }, { "music": "sdds" } ] } void ReadJson() { std::string strValue = "{\"name\":\"xiaoming\",\"like\":[{\"book\":\"json\"},{\"food\":\"apple\"},{\"music\":\"sdds\"}]}"; Json::Reader reader; Json::Value value; if (reader.parse(strValue, value)) { std::string out = value["name"].asString(); std::cout << out << std::endl; const Json::Value arrayObj = value["like"]; for (unsigned int i = 0; i < arrayObj.size(); i++) { if (!arrayObj[i].isMember("book")) continue; out = arrayObj[i]["book"].asString(); std::cout << out; if (i != (arrayObj.size() - 1)) std::cout << std::endl; } } }
序列化Json(生成Json):
void WriteJson() { Json::Value root; Json::Value arrayObj; Json::Value item; item["food"] = "apple"; item["music"] = "JZhou"; item["book"] = "json"; arrayObj.append(item); root["name"] = "xiaoming"; root["like"] = arrayObj; root.toStyledString(); std::string out = root.toStyledString(); std::cout << out << std::endl; }
經由過程JSON方法的socket傳輸
1、客戶端:
#include "json//json.h" #include <WinSock2.h> #pragma comment(lib, "WS2_32.lib") #pragma comment(lib, "json_vc71_libmtd.lib") int main() { Json::Value val; Json::StyledWriter style_write; val["name"] = "xiaoli" ; WSADATA wsaData; SOCKET SendSocket; sockaddr_in RecvAddr; int Port = 27015; //初始化Socket WSAStartup(MAKEWORD(2, 2), &wsaData); //創立Socket對象 SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //設置辦事器地址 RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); std::string SendBuf = style_write.write(val); //向辦事器發送數據 sendto(SendSocket, SendBuf.c_str(), SendBuf.size(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr)); closesocket(SendSocket); WSACleanup(); getchar(); return 0; }
2、辦事器端:
#include <iostream> #include <WinSock2.h> #include "json/json.h" #pragma comment(lib, "WS2_32.lib") #pragma comment(lib, "json_vc71_libmtd.lib") int main() { //初始化socket WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); //創立socket SOCKET RecvSocket; RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //設置辦事器地址 sockaddr_in RecvAddr; int Port = 27015; RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); //綁定socket bind(RecvSocket, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr)); char RecvBuf[1024]; int BufLen = 1024; sockaddr_in SenderAddr; int SendAddrSize = sizeof(SenderAddr); recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR*)&SenderAddr, &SendAddrSize); std::string strName; Json::Value val; Json::Reader reader; if (reader.parse(RecvBuf, val)) { strName = val["name"].asString(); } std::cout << strName << std::endl; closesocket(RecvSocket); WSACleanup(); getchar(); return 0; }