CString strDatas = “https://compliance.his.huawei.com/SpecialManagement/external/product/character/submit“;
CString strAPIURL = strDatas;
CURL *curl;
CString strHtml;
CString strInfo;
strInfo.Format(L"{"url":"%s","jsonStr":%s}“, strAPIURL, Body);
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
USES_CONVERSION;//声明标识符
//char *c_url = T2A(strAPIURL);
curl_slist *head = NULL;
head = curl_slist_append(head, “Content-Type: application/json”);
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, “http://10.11.1.10:1024/user/postRequest”);//地址
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); //设置请求的超时时间
curl_easy_setopt(curl, CURLOPT_POST, 1L);//POST请求
string jsonInfo = ConvertCStringToUTF8(strInfo);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonInfo.c_str()); // 要发送的数据
std::string strdata;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//设置回调函数,用于处理返回的数据
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strdata);
csReturn = “”;
res = curl_easy_perform(curl);
CString m_ErrMsg = L"获取磐石数据失败,请联系管理员”;
if (res != CURLE_OK) {
errMsg = m_ErrMsg;
return FALSE;
}
curl_easy_cleanup(curl);
strHtml = “”;
strHtml = csReturn;
char json_str = (char)(LPCTSTR)strHtml;
cJSON *root = cJSON_Parse(strdata.c_str());
if (root == NULL) {
errMsg = m_ErrMsg;
return FALSE;
} cJSON *code = cJSON_GetObjectItem(root, "code"); if (code == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *data1 = cJSON_GetObjectItem(root, "data"); if (data1 == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *analysisMsg = cJSON_GetObjectItem(root, "analysisMsg"); if (analysisMsg == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *msg = cJSON_GetObjectItem(root, "msg"); if (msg == NULL) { errMsg = m_ErrMsg; return FALSE; } CString strCode = JsonTOCString(code); Sename = JsonTOCString(data1);//评审页面 CString stranalysisMsg = JsonTOCString(analysisMsg); CString strMsg = JsonTOCString(msg); if (csReturn.Find(L"服务调用成功") >= 0 || strCode.Find(L"200") >= 0) { return TRUE; } //{"code":99999,"msg":"特性状态非编制中、已驳回、评审不通过,不允许发起评审!", else if (strCode.Find(L"99999") >= 0) { errMsg = strMsg; return FALSE; } else { return FALSE; }其中CString SpecialCharacteristic::JsonTOCString(cJSON * json)
{
if (!json)
{
return L"“;
}
if (json->type == cJSON_Number)
{
if (json->valueint == json->valuedouble) //int
{
int ival = json->valueint;
CString strNumber;
strNumber.Format(_T(”%d"), ival);
return strNumber;
}
else//double
{
double dval = json->valuedouble;
CString strNumber;
strNumber.Format(_T(“%.2f”), dval);
return strNumber;
}
}
else if (json->type == cJSON_String)
{
string value = json->valuestring;
CString str;
CStringW unicodeStr;
int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0);
if (len > 0) {
LPWSTR buf = unicodeStr.GetBuffer(len);
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, buf, len);
str.Format(L"%ls", buf);
unicodeStr.ReleaseBuffer();
}
return str;
///////////////////////////////////////////////
//std::string value = json->valuestring;
//int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0);
//wchar_t* wstr = new wchar_t[len];
//MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, wstr, len);
//int len2 = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
//char* str = new char[len2];
//WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len2, NULL, NULL);
//CString cstr(str);//特殊符号会给改成其它
//delete[] wstr;
//delete[] str;
////////////////////////////////////////////////
/string value = json->valuestring;
LPCWSTR unicodeStr1 = CA2T(value.c_str(), CP_UTF8);//字段过长会乱码
CString cstr(unicodeStr1);/
}
else if (json->type == cJSON_Array)
{
CString strResult;
for (int i = 0; i < cJSON_GetArraySize(json); i++)
{
cJSON* item = cJSON_GetArrayItem(json, i);
string value = cJSON_Print(item);
CString str;
CStringW unicodeStr;
int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0);
if (len > 0) {
LPWSTR buf = unicodeStr.GetBuffer(len);
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, buf, len);
str.Format(L"%ls", buf);
unicodeStr.ReleaseBuffer();
}
str.Replace(L""“, L”“);
strResult += str;
if (i != cJSON_GetArraySize(json) - 1)
{
strResult += L”,“;
}
}
return strResult;
}
else if (json->type == cJSON_False)
{
return L"false”;
}
else if (json->type == cJSON_True)
{
return L"true";
}
else
{
return L"";
}
}其中static std::string ConvertCStringToUTF8(CString strValue)
{
std::wstring wbuffer;
wbuffer.assign(strValue.GetString(), strValue.GetLength());
/* 获取转换后长度 */
int length = WideCharToMultiByte(CP_UTF8, 0, wbuffer.data(), wbuffer.size(), NULL, 0, NULL, NULL);
/* 获取转换后内容 */
std::string buffer;
buffer.resize(length);
WideCharToMultiByte(CP_UTF8, 0, strValue, -1, (LPSTR)(buffer.data()), length, NULL, NULL);
return(buffer);
}转换为qt程序