cJson

cooolr 于 2022-03-22 发布

解释JSON

#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>

extern char *body;
extern int get(char *url);

int main() {
    char *text;
    cJSON* data = NULL;
    cJSON* headers = NULL;
    cJSON* url = NULL;
    body = (char *)malloc(0);
    get("http://httpbin.org/get");
    data = cJSON_Parse(body);
    headers = cJSON_GetObjectItem(data, "headers");
    text = cJSON_Print(headers);
    url = cJSON_GetObjectItem(data, "url");
    printf("%s\n", url->valuestring);
    free(body);
    cJSON_Delete(data);
    return 0;
}