int powerswitch[5];
char *timer[5];
int enable[5];
int isvalid[5];
cJSON * data_JSON = cJSON_Parse(request);
if(cJSON_GetObjectItem(data_JSON,"LocalTimer") != NULL)
{
cJSON *localtime_arr = cJSON_GetObjectItem(data_JSON,"LocalTimer");
cJSON *arr_item = localtime_arr->child;
int i=0;
while(arr_item!= NULL)
{
powerswitch[i]=cJSON_GetObjectItem(arr_item,"PowerSwitch")->valueint;
timer[i]=cJSON_GetObjectItem(arr_item,"Timer")->valuestring;
enable[i]=cJSON_GetObjectItem(arr_item,"Enable")->valueint;
isvalid[i]=cJSON_GetObjectItem(arr_item,"IsValid")->valueint;
printf("PowerSwitch: %d Timer: %s Enable: %d IsValid: %d",powerswitch[i],(char *)timer[i],enable[i],isvalid[i]);
arr_item=arr_item->next;
i++;
if(i>=5){break;}
}
}
本文介绍了一种从JSON数组中解析并读取特定字段数据的方法,通过使用cJSON库,展示了如何逐项访问数组元素,并提取PowerSwitch、Timer、Enable和IsValid等属性的值。
578

被折叠的 条评论
为什么被折叠?



