//解析imei字段字符串内容 cJSON *pimeiAdress = cJSON_GetObjectItem(pJsonRoot, "imei"); //判断imei字段是否json格式 if (pimeiAdress) { //判断mac字段是否string类型 if (cJSON_IsString(pimeiAdress)) iot_debug_print("[cJSON_Test] get imeiAdress:%s", pimeiAdress->valuestring); } else iot_debug_print("[cJSON_Test] get imeiAdress failed");
//解析Num字段int内容 cJSON *pNumber = cJSON_GetObjectItem(pJsonRoot, "Num"); //判断Num字段是否存在 if (pNumber) { //判断mac字段是否数字整型类型 if (cJSON_IsNumber(pNumber)) iot_debug_print("[cJSON_Test] get Num:%d", pNumber->valueint); } else iot_debug_print("[cJSON_Test] get Num failed");
//解析value字段内容,判断是否为json cJSON *pValue = cJSON_GetObjectItem(pJsonRoot, "Value"); if (pValue) { //进一步剖析里面的name字段:注意这个根节点是 pValue cJSON *pName = cJSON_GetObjectItem(pValue, "name"); if (pName) { if (cJSON_IsString(pName)) iot_debug_print("[cJSON_Test] get value->Name:%s", pName->valuestring); } else iot_debug_print("[cJSON_Test] get pValue->pName failed");
//进一步剖析里面的age字段:注意这个根节点是 pValue cJSON *pAge = cJSON_GetObjectItem(pValue, "age"); if (pAge) { if (cJSON_IsNumber(pAge)) iot_debug_print("[cJSON_Test] get value->Age:%d", pAge->valueint); } else iot_debug_print("[cJSON_Test] get pValue->pAge failed");
//进一步剖析里面的blog字段:注意这个根节点是 pValue cJSON *pBlog = cJSON_GetObjectItem(pValue, "blog"); if (pBlog) { if (cJSON_IsString(pBlog)) iot_debug_print("[cJSON_Test] get value->pBlog:%s", pBlog->valuestring); } else iot_debug_print("[cJSON_Test] get pValue->pBlog failed"); } else iot_debug_print("[cJSON_Test] get pValue failed");
//剖析数组 cJSON *pArry = cJSON_GetObjectItem(pJsonRoot, "hexArry"); if (pArry) { //获取数组长度 int arryLength = cJSON_GetArraySize(pArry); iot_debug_print("[cJSON_Test] get arryLength:%d", arryLength); //逐个打印 int i; for (i = 0; i < arryLength; i++) iot_debug_print("[cJSON_Test] get cJSON_GetArrayItem(pArry, %d)= %d", i, cJSON_GetArrayItem(pArry, i)->valueint); } else iot_debug_print("[cJSON_Test] get pArry failed");
//解析imei字段字符串内容 cJSON *pimeiAdress = cJSON_GetObjectItem(pJsonRoot, "imei"); //判断imei字段是否json格式 if (pimeiAdress) { //判断mac字段是否string类型 if (cJSON_IsString(pimeiAdress)) iot_debug_print("[cJSON_Test] get imeiAdress:%s", pimeiAdress->valuestring); } else iot_debug_print("[cJSON_Test] get imeiAdress failed");
//解析Num字段int内容 cJSON *pNumber = cJSON_GetObjectItem(pJsonRoot, "Num"); //判断Num字段是否存在 if (pNumber) { //判断mac字段是否数字整型类型 if (cJSON_IsNumber(pNumber)) iot_debug_print("[cJSON_Test] get Num:%d", pNumber->valueint); } else iot_debug_print("[cJSON_Test] get Num failed");
//解析value字段内容,判断是否为json cJSON *pValue = cJSON_GetObjectItem(pJsonRoot, "Value"); if (pValue) { //进一步剖析里面的name字段:注意这个根节点是 pValue cJSON *pName = cJSON_GetObjectItem(pValue, "name"); if (pName) { if (cJSON_IsString(pName)) iot_debug_print("[cJSON_Test] get value->Name:%s", pName->valuestring); } else iot_debug_print("[cJSON_Test] get pValue->pName failed");
//进一步剖析里面的age字段:注意这个根节点是 pValue cJSON *pAge = cJSON_GetObjectItem(pValue, "age"); if (pAge) { if (cJSON_IsNumber(pAge)) iot_debug_print("[cJSON_Test] get value->Age:%d", pAge->valueint); } else iot_debug_print("[cJSON_Test] get pValue->pAge failed");
//进一步剖析里面的blog字段:注意这个根节点是 pValue cJSON *pBlog = cJSON_GetObjectItem(pValue, "blog"); if (pBlog) { if (cJSON_IsString(pBlog)) iot_debug_print("[cJSON_Test] get value->pBlog:%s", pBlog->valuestring); } else iot_debug_print("[cJSON_Test] get pValue->pBlog failed"); } else iot_debug_print("[cJSON_Test] get pValue failed");
//剖析数组 cJSON *pArry = cJSON_GetObjectItem(pJsonRoot, "hexArry"); if (pArry) { //获取数组长度 int arryLength = cJSON_GetArraySize(pArry); iot_debug_print("[cJSON_Test] get arryLength:%d", arryLength); //逐个打印 int i; for (i = 0; i < arryLength; i++) iot_debug_print("[cJSON_Test] get cJSON_GetArrayItem(pArry, %d)= %d", i, cJSON_GetArrayItem(pArry, i)->valueint); } else iot_debug_print("[cJSON_Test] get pArry failed");