Explain the use of C++ JSON static link library JsonCpp
- 2020-05-09 18:59:55
- OfStack
JsonCpp deployment method:
In http: / / sourceforge net/projects jsoncpp/download the latest version of jsoncpp library source code.
Then unzip jsoncpp-src-version number -tar.gz, open jsoncpp.sln in makefiles and compile it. Then there will be a.lib static link library in vs71\debug\lib_json in build folder.
JsonCpp mainly includes three types of class:Value Reader Writer.
All object and class names in jsoncpp are in namespace json, including json.h
Json::Value : can represent all supported types, such as int, double,string, object, etc Json::Reader: parses a file stream or string creation into Json::Value, mainly using the parse function. Json::Writer : , as opposed to JsonReader, converts Json::Value into a string stream, etc.Note :Jsoncpp's Json::Writer class is a pure virtual class and cannot be used directly. Here we use the subclasses of Json::Writer: Json::FastWriter, Json::StyledWriter, Json::StyledStreamWriter.
Deserialize Json (parse 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;
}
}
}
Serialize Json(generate 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;
}
socket transmission via JSON
1. Client:
#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;
// Initialize the Socket
WSAStartup(MAKEWORD(2, 2), &wsaData);
// create Socket object
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Set the server address
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);
// Send data to the server
sendto(SendSocket, SendBuf.c_str(), SendBuf.size(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
closesocket(SendSocket);
WSACleanup();
getchar();
return 0;
}
2. Server side:
#include <iostream>
#include <WinSock2.h>
#include "json/json.h"
#pragma comment(lib, "WS2_32.lib")
#pragma comment(lib, "json_vc71_libmtd.lib")
int main()
{
// Initialize the socket
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
// create socket
SOCKET RecvSocket;
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Set the server address
sockaddr_in RecvAddr;
int Port = 27015;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// The binding 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;
}