stm32 cjson 解析json数据实例
char* pszJsonText = "{\"result\":{\"id\":1,\"name\":\"aa\",\"datetime\":\"2016-04-25 16:38:54\",\"status\":1}, \ \"code\":10000,\"message\":\"ok\",\"sign\":901498365}"; typedef struct { u32 cID; char *pName; char *datetime; u8 cStatus; }SCAN_CODE_LIST_MEM_TypeDef; SCAN_CODE_LIST_MEM_TypeDef g_stScanCodeListMem; u8 ParseScanCodeJson(const char* pszJsonText) { cJSON *root, *result; u32 code=0; u8 ret = FALSE; //将json数据解析成json结构体 root= cJSON_Parse(pszJsonText); if (root) { //从根节点获取code元素的值 int型 code = cJSON_GetObjectItem(root, "code")->valueint; if(code != 10001) { //从根节点获取result节点 result = cJSON_GetObjectItem(root, "result"); if (result) { //从result节点获取各个元素的值 g_stScanCodeListMem.cID = cJSON_GetObjectItem(result, "id")->valueint; //int 型 g_stScanCodeListMem.pName = cJSON_GetObjectItem(result, "name")->valuestring;//字符串型 g_stScanCodeListMem.datetime = cJSON_GetObjectItem(result, "datetime")->valuestring;//字符串型 g_stScanCodeListMem.cStatus = cJSON_GetObjectItem(result, "status")->valueint;//int 型 //保存扫描用户的名字 把id值也作为一个参数存入 WriteScanCodeName(g_stScanCodeListMem.pName, g_stScanCodeListMem.cID); #ifdef DEBUG_APP_TASK printf("\r\n"); printf(" cID:%d ", g_stScanCodeListMem.cID); printf(" pName:%s", g_stScanCodeListMem.pName); printf(" datetime:%s ", g_stScanCodeListMem.datetime); printf(" cStatus:%d ", g_stScanCodeListMem.cStatus); printf("\r\n"); #endif ret = TRUE; } } //释放资源 cJSON_Delete(root); } return ret; }
微信扫码关注
更新实时通知