今天刚好使用cJSON进行解析json数组,json数组两种格式都有,在此记录一下
1 第一种格式的json数组
格式如下
GpsUploadPolicy":{"limit":"180","busyPolicy":["5","100"],"idlePolicy":["10","50"]}
解析idlePolicy数组方法如下:
cJSON * idle_policy_arry = c_json_parse_object(gps_upload_policy_cJSON, "idlePolicy");
if (idle_policy_arry != NULL) {
int idle_arry_size = cJSON_GetArraySize(idle_policy_arry);
for (int j = 0; j < idle_arry_size; j++) {
char *value = cJSON_GetArrayItem(idle_policy_arry, j);
}
}
2 第二种格式的json数组
格式如下:
"codeRateSetting":[{"resolution":"480P","codeRate":"512"},{"resolution":"720P","codeRate":"1024"},{"resolution":"1080P","codeRate":"3072"}]
这种解析方法如下
cJSON *code_rate_setting_arry = cJSON_GetObjectItem( code_rate_setting_cJSON, "codeRateSetting");
if( NULL != code_rate_setting_arry ){
cJSON *code_rate_setting_item = code_rate_setting_arry->child;
while( code_rate_setting_item != NULL ){
char * resolution = cJSON_GetObjectItem( code_rate_setting_item , "resolution")->valuestring ;
char * coderate = cJSON_GetObjectItem( code_rate_setting_item , "coderate")->valuestring ;
printf("resolution= %s, coderate= %s\n",resolution,coderate);
code_rate_setting_item = code_rate_setting_item->next ;
}
}