json解析

This commit is contained in:
aixiao 2022-09-22 15:42:40 +08:00
parent ed6e97ba27
commit c3031b8da6
2 changed files with 30 additions and 3 deletions

Binary file not shown.

View File

@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"
#include <cjson/cJSON.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -65,7 +65,34 @@ size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
strncpy(buf, ptr, size * nmemb);
cJSON *json = cJSON_Parse(buf);
printf("data:%s\n", cJSON_Print(json));
cJSON *result = cJSON_GetObjectItem(json, "result");
cJSON *face_list = cJSON_GetObjectItem(result, "face_list");
// 数组
int skill_array_size = cJSON_GetArraySize(face_list);
for (int i = 0; i < skill_array_size; i++) {
cJSON *cjson_skill_item = cJSON_GetArrayItem(face_list, i);
//printf("%s\n", cJSON_Print(cjson_skill_item));
cJSON *age = cJSON_GetObjectItem(cjson_skill_item, "age");
printf("年龄: %s\n", cJSON_Print(age));
cJSON *gender = cJSON_GetObjectItem(cjson_skill_item, "gender");
cJSON *type = cJSON_GetObjectItem(gender, "type");
if (0 == strcmp(type->valuestring, "female"))
{
printf("性别: 女性\n");
}
else
{
printf("性别: 男性\n");
}
cJSON *beauty = cJSON_GetObjectItem(cjson_skill_item, "beauty");
printf("美丽值: %s\n", cJSON_Print(beauty));
}
if (strstr(cJSON_Print(json), "SUCCESS") == NULL) {
*((double *)stream) = 0;
} else {