一、说明
说明是JSON,JSON该怎么用,不再详细赘述,可以查看本人的这篇文章:配置文件:CJSON的使用
1.1 类型说明
枚举了一些类型,
登录信息
在线信息
登出信息
其他用户是否在线信息
发送信息
接收信息
联系人信息
所有用户信息
所有部门信息
主管信息
部门员工信息
指定联系人信息
1.2 流程
以登录信息的JSON字符串为例
通常有两部分构成:
- 消息类型
- 消息内容
{
"msg_type": 0,
"LOGIN": {
"id": 7,
"password": "123456"
}
}
所以在接收消息时,我们先判断类型,再根据类型进行解析,最后针对类型进行数据返回
二、封装类
.cpp文件
#include "JsonCtrl.h"
JsonCtrl::JsonCtrl()
{
m_login = nullptr;
m_logout = nullptr;
m_root = nullptr;
m_recv = nullptr;
m_send = nullptr;
m_contacts = nullptr;
m_allAccount = nullptr;
m_allDepartment = nullptr;
}
JsonCtrl::~JsonCtrl()
{
}
CString JsonCtrl::Produce(int id, CString password)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", LOGIN_MSG);
SplitLogIn(id, password);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(ReplyMsg msg)
{
NewJson();
cJSON_AddNumberToObject(m_root, "RESULT", msg.result);
cJSON_AddStringToObject(m_root, "DATA", msg.data);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(int id)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", LOGOUT_MSG);
SplitLogOut(id);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(SendDataInfo info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", SEND_MSG);
SplitSend(info);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(RecvDataInfo info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", RECV_MSG);
SplitRecv(info);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(ContactsInfo info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", CONTACT_MSG);
SplitContact(info);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::Produce(AccountInfo info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", MANAGER_MSG);
cJSON_AddNumberToObject(m_root, "account_id", info.account_id);
cJSON_AddStringToObject(m_root, "name", info.name);
cJSON_AddStringToObject(m_root, "password", info.password);
cJSON_AddStringToObject(m_root, "sex", info.sex);
cJSON_AddNumberToObject(m_root, "age", info.age);
cJSON_AddNumberToObject(m_root, "DPID", info.department_id);
cJSON_AddStringToObject(m_root, "DPNAME", info.department_name);
cJSON_AddStringToObject(m_root, "headship", info.headship);
cJSON_AddStringToObject(m_root, "office_phone", info.office_phone);
cJSON_AddStringToObject(m_root, "handset", info.hand_set);
cJSON_AddStringToObject(m_root, "email", info.email);
cJSON_AddStringToObject(m_root, "address", info.address);
cJSON_AddStringToObject(m_root, "picture", info.picture);
cJSON_AddNumberToObject(m_root, "online", info.online);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::ProduceAllAccount(vector<AccountInfo> info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", ALL_ACCOUNT_MSG);
SplitAllAcount(info);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
CString JsonCtrl::ProduceAllDepartment(vector<DepartmentInfo> info)
{
NewJson();
cJSON_AddNumberToObject(m_root, "msg_type", ALL_DEPARTMENT_MSG);
SplitAllDepartment(info);
m_result = cJSON_Print(m_root);
DeleteJson();
return m_result;
}
bool JsonCtrl::Parse(CString data, CString& title)
{
cJSON* root = cJSON_Parse(data);
cJSON* result = cJSON_GetObjectItem(root, "RESULT");
cJSON* date = cJSON_GetObjectItem(root, "DATA");
if (!result->valueint) {
return false;
}
if (!CString(date->valuestring).IsEmpty()) {
title = CString(date->valuestring);
}
return true;
}
bool JsonCtrl::Parse(CString data, SendDataInfo& info)
{
cJSON* root = cJSON_Parse(data);
cJSON* send = cJSON_GetObjectItem(root, "SEND");
info.recv_id = cJSON_GetObjectItem(send, "recv_id")->valueint;
info.time = cJSON_GetObjectItem(send, "time")->valuestring;
info.SendData = cJSON_GetObjectItem(send, "send_data")->valuestring;
return true;
}
int JsonCtrl::ParseType(CString data)
{
cJSON* root = cJSON_Parse(data);
cJSON* result = cJSON_GetObjectItem(root, "msg_type");
return result->valueint;
}
bool JsonCtrl::ParseDepartmentId(CString data, int& id)
{
cJSON* root = cJSON_Parse(data);
if (!root) {
return false;
}
cJSON* result = cJSON_GetObjectItem(root, "dpId");
if (!result) {
return false;
}
id = result->valueint;
return true;
}
bool JsonCtrl::ParseRequestId(CString data,int& id)
{
cJSON* root = cJSON_Parse(data);
if (!root) {
return false;
}
cJSON* result = cJSON_GetObjectItem(root, "dpId");
if (!result) {
return false;
}
id = result->valueint;
return true;
}
bool JsonCtrl::ParseId(CString data, int& id)
{
cJSON* root = cJSON_Parse(data);
if (!root) {
return false;
}
cJSON* result = cJSON_GetObjectItem(root, "id");
if (!result) {
return false;
}
id = result->valueint;
return true;
}
bool JsonCtrl::ParseLogIn(CString data,int &account_id)
{
cJSON* root = cJSON_Parse(data);
cJSON* result = cJSON_GetObjectItem(root, "LOGIN");
cJSON* pwd = cJSON_GetObjectItem(result, "password");
cJSON* id = cJSON_GetObjectItem(result, "id");
AccountInfo info;
bool res = m_acCtrl.GetAccountInfo(info, id->valueint);
if (!res) {
return false;
}
if (0!=info.password.Compare(pwd->valuestring)) {
return false;
}
account_id = id->valueint;
return true;
}
void JsonCtrl::NewJson()
{
m_login = cJSON_CreateObject();
m_logout = cJSON_CreateObject();
m_root = cJSON_CreateObject();
m_send = cJSON_CreateObject();
m_recv = cJSON_CreateObject();
m_contacts = cJSON_CreateObject();
m_allAccount = cJSON_CreateObject();
m_allDepartment = cJSON_CreateObject();
}
void JsonCtrl::SplitLogIn(int id, CString password)
{
cJSON_AddNumberToObject(m_login, "id", id);
cJSON_AddStringToObject(m_login, "password", password);
cJSON_AddItemToObject(m_root, "LOGIN", m_login);
}
void JsonCtrl::SplitLogOut(int id)
{
cJSON_AddNumberToObject(m_login, "id", id);
cJSON_AddItemToObject(m_root, "LOGOUT", m_logout);
}
void JsonCtrl::SplitSend(SendDataInfo info)
{
cJSON_AddNumberToObject(m_send, "recv_id", info.recv_id);
cJSON_AddStringToObject(m_send, "time", info.time);
cJSON_AddStringToObject(m_send, "send_data", info.SendData);
cJSON_AddItemToObject(m_root, "SEND", m_send);
}
void JsonCtrl::SplitRecv(RecvDataInfo info)
{
cJSON_AddNumberToObject(m_recv, "send_id", info.send_id);
cJSON_AddStringToObject(m_recv, "time", info.time);
cJSON_AddStringToObject(m_recv, "send_data", info.data);
cJSON_AddItemToObject(m_root, "RECV", m_recv);
}
void JsonCtrl::SplitContact(ContactsInfo info)
{
cJSON_AddNumberToObject(m_contacts, "id", info.id);
cJSON_AddNumberToObject(m_contacts, "department_id", info.departmentId);
cJSON_AddStringToObject(m_contacts, "name", info.name);
cJSON_AddStringToObject(m_contacts, "department_name", info.departmentName);
cJSON_AddNumberToObject(m_contacts, "onlIne", info.Online);
cJSON_AddStringToObject(m_contacts, "data", info.SendData);
cJSON_AddItemToObject(m_root, "CONTACTS", m_contacts);
}
void JsonCtrl::SplitAllAcount(vector<AccountInfo> info)
{
cJSON_AddNumberToObject(m_allAccount, "account_number", info.size());
for (int i = 0; i < info.size();i++) {
cJSON_AddNumberToObject(m_allAccount, (string("account_id") + to_string(i)).c_str(), info.at(i).account_id);
cJSON_AddStringToObject(m_allAccount, (string("name") + to_string(i)).c_str(), info.at(i).name);
cJSON_AddStringToObject(m_allAccount, (string("password") + to_string(i)).c_str(), info.at(i).password);
cJSON_AddStringToObject(m_allAccount, (string("sex") + to_string(i)).c_str(), info.at(i).sex);
cJSON_AddNumberToObject(m_allAccount, (string("age") + to_string(i)).c_str(), info.at(i).age);
cJSON_AddNumberToObject(m_allAccount, (string("DPID") + to_string(i)).c_str(), info.at(i).department_id);
cJSON_AddStringToObject(m_allAccount, (string("DPNAME") + to_string(i)).c_str(), info.at(i).department_name);
cJSON_AddStringToObject(m_allAccount, (string("headship") + to_string(i)).c_str(), info.at(i).headship);
cJSON_AddStringToObject(m_allAccount, (string("office_phone") + to_string(i)).c_str(), info.at(i).office_phone);
cJSON_AddStringToObject(m_allAccount, (string("handset") + to_string(i)).c_str(), info.at(i).hand_set);
cJSON_AddStringToObject(m_allAccount, (string("email") + to_string(i)).c_str(), info.at(i).email);
cJSON_AddStringToObject(m_allAccount, (string("address") + to_string(i)).c_str(), info.at(i).address);
cJSON_AddStringToObject(m_allAccount, (string("picture") + to_string(i)).c_str(), info.at(i).picture);
cJSON_AddNumberToObject(m_allAccount, (string("online") + to_string(i)).c_str(), info.at(i).online);
}
cJSON_AddItemToObject(m_root, "ALLACCOUNT", m_allAccount);
}
void JsonCtrl::SplitAllDepartment(vector<DepartmentInfo> info)
{
cJSON_AddNumberToObject(m_allDepartment, "department_number", info.size());
for (int i = 0; i < info.size(); i++) {
cJSON_AddNumberToObject(m_allDepartment, (string("id") + to_string(i)).c_str(), info.at(i).id);
cJSON_AddStringToObject(m_allDepartment, (string("name") + to_string(i)).c_str(), info.at(i).name);
cJSON_AddNumberToObject(m_allDepartment, (string("pid") + to_string(i)).c_str(), info.at(i).parentId);
cJSON_AddNumberToObject(m_allDepartment, (string("mid") + to_string(i)).c_str(), info.at(i).managerId);
}
cJSON_AddItemToObject(m_root, "ALLDP", m_allDepartment);
}
void JsonCtrl::DeleteJson()
{
if(m_root) cJSON_Delete(m_root);
}
.h文件
#pragma once
#include "Common.h"
#include "cJSON.h"
#include <vector>
#include <string>
#include "AccountCtrl.h"
#include "DepartmentCtrl.h"
using namespace std;
class JsonCtrl
{
public:
JsonCtrl();
~JsonCtrl();
public:
CString Produce(int id, CString password);
CString Produce(ReplyMsg msg);
CString Produce(int id);
CString Produce(SendDataInfo info);
CString Produce(RecvDataInfo info);
CString Produce(ContactsInfo info);
CString Produce(AccountInfo info);
CString ProduceAllAccount(vector<AccountInfo> info);
CString ProduceAllDepartment(vector<DepartmentInfo> info);
int ParseType(CString data);
bool ParseDepartmentId(CString data, int& id);
bool ParseRequestId(CString data, int &id);
bool ParseId(CString data, int& id);
bool ParseLogIn(CString data,int& id);
bool Parse(CString data, CString &title);
bool Parse(CString data, SendDataInfo &info);
bool Parse(CString data, RecvDataInfo &info);
bool Parse(CString data, ContactsInfo &info);
bool Parse(CString data, vector<DepartmentInfo>& info);
bool Parse(CString data, vector<AccountInfo>& info);
private:
CString m_result;
cJSON* m_root;
cJSON* m_login;
cJSON* m_logout;
cJSON* m_send;
cJSON* m_recv;
cJSON* m_contacts;
cJSON* m_allAccount;
cJSON* m_allDepartment;
void NewJson();
void SplitLogIn(int id,CString password);
void SplitLogOut(int id);
void SplitSend(SendDataInfo info);
void SplitRecv(RecvDataInfo info);
void SplitContact(ContactsInfo info);
void SplitAllAcount(vector<AccountInfo> info);
void SplitAllDepartment(vector<DepartmentInfo> info);
void DeleteJson();
private:
AccountCtrl m_acCtrl;
DepartmentCtrl m_dpCtrl;
};