#include "APIKey.h" #include "cJSON/cJSON.h" #include "request.h" #include #include void parseWeather(const char *data); int main(int argc, char *argv[]) { request req; response res; simpleHttpInit(&req); char url[256] = "https://api.openweathermap.org/data/2.5/" "weather?lat=51.52&lon=-0.10&units=imperial&appid="; strcat(url, API_KEY); // req.url = "https://catfact.ninja/fact"; req.url = url; error err = simpleHttpRequest(&req, &res, JSON, GET); if (err != NO_ERROR) { printf("error: %s\n", simpleHttpErrorString(err)); return 1; } if (res.code != 200) { printf("response code %ld\n", res.code); printf("response body:\n%s\n", res.body); return 1; } parseWeather(res.body); simpleHttpClose(&req, &res); return 0; } void parseWeather(const char *data) { cJSON *json = cJSON_Parse(data); if (!json) { const char *err = cJSON_GetErrorPtr(); if (err) { printf("%s\n", err); } else { printf("error parsing JSON\n"); } return; } const cJSON *weatherArray = NULL; const cJSON *weather = NULL; const cJSON *main = NULL; const cJSON *temp = NULL; weatherArray = cJSON_GetObjectItemCaseSensitive(json, "weather"); cJSON_ArrayForEach(weather, weatherArray) { cJSON *description = cJSON_GetObjectItemCaseSensitive(weather, "description"); if (!cJSON_IsString(description)) { printf("error parsing description\n"); return; } printf("Weather description: %s\n", description->valuestring); } main = cJSON_GetObjectItemCaseSensitive(json, "main"); if (!main) { printf("Error parsing main\n"); return; } temp = cJSON_GetObjectItemCaseSensitive(main, "temp"); if (!temp) { printf("error parsing temp\n"); return; } printf("Temp: %.2f\n", temp->valuedouble); }