时间:2021-05-11 08:53:44 | 栏目:C代码 | 点击:次
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; }