小编And*_*hez的帖子

如何在 C 中使用 curl 并解析 JSON 响应

下面的代码编译并打印响应。

我的问题是:作为代表对象的字符串的响应,如何将“res”转换为字符串或直接转换为 JSON 对象?

#include <stdio.h>
#include <curl/curl.h>
#include <json-c/json.h>

int main(int argc, char **argv) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();

    if(curl) {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/system/genpass");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "length: 20");
        headers = curl_slist_append(headers, "numbers: true");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        res = curl_easy_perform(curl);
        printf("%u",res);
    }

    curl_easy_cleanup(curl);
}
Run Code Online (Sandbox Code Playgroud)

c parsing curl json-c

2
推荐指数
1
解决办法
3795
查看次数

标签 统计

c ×1

curl ×1

json-c ×1

parsing ×1