1.json数据解析
样例如下;
{“cmd”: “start”, “nonce”: “11223344556”}
//create json root
cJSON *pJsonRoot = NULL;
unsigned char data[ ] = {"cmd": "start", "nonce": "11223344556"};
pJsonRoot= cJSON_Parse((const char*)data);
if(pJsonRoot == NULL)
{
qDebug() << "c json parse msg string failed";
return;
}
cJSON *pJsoncmd = cJSON_GetObjectItem(pJsonRoot, "cmd");
if(pJsoncmd == NULL)
{
qDebug() << "cjson parse cmd failed";
cJSON_Delete(pJsonRoot);
return;
}
QString cmd= jsonNonce->valuestring;
cJSON *jsonNonce = cJSON_GetObjectItem(pJsonRoot, "nonce");
if(jsonNonce == NULL)
{
qDebug() << "cjson parse nonce failed";
cJSON_Delete(pJsonRoot);
return;
}
QString nonce = jsonNonce->valuestring;
2.json数据打包
样例如下:
{“event”: “downloading”, “timestamp”: 1604289412, “taskid”: “1234”, “progress”: 0, “nonce”: “11223344500”}
string postParams, nonce;
unsigned char buffer[NETWORK_MAXBUF];
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "event", "downloading");
QString timestamp = QString::number(QDateTime::currentMSecsSinceEpoch() / 1000);
cJSON_AddStringToObject(root, "timestamp", timestamp.toStdString().c_str());
cJSON_AddStringToObject(root, "taskid", "1234");
cJSON_AddNumberToObject(root, "progress", 0);
cJSON_AddStringToObject(root, "nonce", "12345678");
char* out = cJSON_PrintUnformatted(root);
postParams = (string)out;
int datamessagelen = generateMessage(buffer, 1, REPORT_REQUEST, getId(true), 0, postParams);
ssl_fun->sslWriteData(buffer, datamessagelen);
if(out!=NULL)
free(out);
cJSON_Delete(root);
版权声明:本文为qq_32348883原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。