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 <stdio.h>
#include <string.h> #include <string.h>
#include <curl/curl.h> #include <curl/curl.h>
#include "cJSON.h" #include <cjson/cJSON.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.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); strncpy(buf, ptr, size * nmemb);
cJSON *json = cJSON_Parse(buf); 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) { if (strstr(cJSON_Print(json), "SUCCESS") == NULL) {
*((double *)stream) = 0; *((double *)stream) = 0;
} else { } else {
@ -99,7 +126,7 @@ int post_faceMatch(char *access_token, char *file)
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POST, 1);
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8"); headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json)); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback);