-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogfact.cpp
68 lines (53 loc) · 1.52 KB
/
dogfact.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "httplib.h"
#include "picojson.h"
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
// Make API url
const std::string url = "https://dog-api.kinduff.com";
std::string path = "/api/facts?number=";
// Get number of facts
if (argc == 2) {
if (atoi(argv[1])) {
path += argv[1];
} else {
std::cerr << "Error: integer expected" << std::endl;
return 1;
}
}
// JSON input
std::string json_data;
httplib::Client cli(url);
if (auto res = cli.Get(path)) {
if (res->status == 200) {
json_data = res->body;
}
} else {
auto err = res.error();
std::cerr << "HTTP error: " << httplib::to_string(err) << std::endl;
return 2;
}
// Parse the JSON
picojson::value json_value;
std::string err = picojson::parse(json_value, json_data);
// Check for parsing errors
if (!err.empty()) {
std::cerr << "Error parsing JSON: " << err << std::endl;
return 3;
}
// Access the parsed JSON
picojson::object& json_object = json_value.get<picojson::object>();
// Access the "facts" array
const picojson::array& facts_array = json_object["facts"].get<picojson::array>();
// Iterate through the facts array and print each fact
std::cout << "Facts:" << std::endl;
for (const picojson::value& fact_value : facts_array) {
std::string fact = fact_value.get<std::string>();
std::cout << "- " << fact << std::endl;
}
// Access the "success" boolean value
bool success = json_object["success"].get<bool>();
std::cout << "Success: " << (success ? "true" : "false") << std::endl;
return 0;
}