一、生成json结构体:
{
"function": [ // pJsonDetail = cJSON_CreateArray();cJSON_AddItemToObject(pstJsonhead, "function", pJsonDetail);
"function": [ // pJsonDetail = cJSON_CreateArray();cJSON_AddItemToObject(pstJsonhead, "function", pJsonDetail);
{
//pJsonDstDetail = cJSON_CreateObject(); cJSON_AddItemReferenceToArray(pJsonDetail, pJsonDstDetail);
"name": "4GDayConsumeFlow",
"code": 1798,
"count": 343,
"date": 127
}
"name": "4GDayConsumeFlow",
"code": 1798,
"count": 343,
"date": 127
}
],
"iccid": "89860617030077961780",
"uuid": "4DC96C04A542E69914A50607B6CEE9660E9403BCEDF6C375932CE8A16AE68A65127366F54A3AAAE96D6A5B4CA3984233"
}
"iccid": "89860617030077961780",
"uuid": "4DC96C04A542E69914A50607B6CEE9660E9403BCEDF6C375932CE8A16AE68A65127366F54A3AAAE96D6A5B4CA3984233"
}
UINT32 LastFlowInfoToJson(cJSON * pstJsonhead,CHAR * pszSimICCID,UINT64 ulLastTime,DOUBLE slDurationFlow,CHAR * g_szUuid)
{
cJSON *pJsonDstDetail = NULL;
cJSON *pJsonDetail = NULL;
pJsonDetail = cJSON_CreateArray();
if (pJsonDetail == NULL)
{
WEB_ErrorTRACE(("cJSON_CreateObject Error\n"));
print_info("cJSON_CreateObject Error\n");
return VOS_ERR;
}
cJSON_AddItemToObject(pstJsonhead, "function", pJsonDetail);
pJsonDstDetail = cJSON_CreateObject();
if(!pJsonDstDetail)
{
print_info(" cJSON_Parse error: %s\n", cJSON_GetErrorPtr());
return VOS_ERROR;
}
cJSON_AddStringToObject(pJsonDstDetail, "name","4GDayConsumeFlow" );
cJSON_AddNumberToObject(pJsonDstDetail, "code", 0x706);
cJSON_AddNumberToObject(pJsonDstDetail, "count", slDurationFlow);
cJSON_AddNumberToObject(pJsonDstDetail, "date", ulLastTime);
cJSON_AddItemReferenceToArray(pJsonDetail, pJsonDstDetail);
cJSON_AddStringToObject(pstJsonhead, "iccid", pszSimICCID);
cJSON_AddStringToObject(pstJsonhead,"uuid", g_szUuid);
}
{
cJSON *pJsonDstDetail = NULL;
cJSON *pJsonDetail = NULL;
pJsonDetail = cJSON_CreateArray();
if (pJsonDetail == NULL)
{
WEB_ErrorTRACE(("cJSON_CreateObject Error\n"));
print_info("cJSON_CreateObject Error\n");
return VOS_ERR;
}
cJSON_AddItemToObject(pstJsonhead, "function", pJsonDetail);
pJsonDstDetail = cJSON_CreateObject();
if(!pJsonDstDetail)
{
print_info(" cJSON_Parse error: %s\n", cJSON_GetErrorPtr());
return VOS_ERROR;
}
cJSON_AddStringToObject(pJsonDstDetail, "name","4GDayConsumeFlow" );
cJSON_AddNumberToObject(pJsonDstDetail, "code", 0x706);
cJSON_AddNumberToObject(pJsonDstDetail, "count", slDurationFlow);
cJSON_AddNumberToObject(pJsonDstDetail, "date", ulLastTime);
cJSON_AddItemReferenceToArray(pJsonDetail, pJsonDstDetail);
cJSON_AddStringToObject(pstJsonhead, "iccid", pszSimICCID);
cJSON_AddStringToObject(pstJsonhead,"uuid", g_szUuid);
}
pJsonMsg =cJSON_Print(pstJsonhead);
cJSON_Delete(pstJsonhead); //pJsonDstDetail ,pJsonDetail不需要单独释放
free(4GDayConsumeFlow); //仅仅拷贝一份,所以需要单独释放
free(pszSimICCID);
free(g_szUuid);