Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. #include <cpr/cpr.h>
  5. #include <json.hpp>
  6.  
  7. void appInit(char *args) {
  8.  
  9. }
  10.  
  11.  
  12. int main(int argc, char *argv) {
  13.  
  14.     if (argc > 1) {
  15.         appInit(argv);
  16.     } else {
  17.         std::cout << "Usage: a.out [search keyword]" << std::endl;
  18.     }
  19.  
  20.     int limit = 3;
  21.  
  22.     cpr::Parameters parameters{{"query", input},
  23.                         {"limit", "3"},
  24.                         {"indent", "true"},
  25.                         {"key", "AIzaSyA5Xr82w7LrJgaLPb99P4kVZzFlxYuPhng"}};
  26.  
  27.     auto response = cpr::Get(cpr::Url{
  28.         "https://kgsearch.googleapis.com/v1/entities:search"}, parameters);
  29.  
  30.     auto json = nlohmann::json::parse(response.text);
  31.     json.flatten();
  32.  
  33.     for (int i=0; i<limit; i++) {
  34.         std::ostringstream oss;
  35.         std::cout << json["/itemListElement/0/result/name"_json_pointer];
  36.         //std::cout << json["/itemListElement/i/result/@type/0"_json_pointer] << std::endl;
  37.         //std::cout << json["/itemListElement/i/result/@type/1"_json_pointer] << std::endl;
  38.         std::cout << json["/itemListElement/0/result/description"_json_pointer] << std::endl;
  39.         std::cout << json["/itemListElement/0/result/detailedDescription/articleBody"_json_pointer] << std::endl;
  40.         std::cout << json["/itemListElement/0/result/image/url"_json_pointer] << std::endl;
  41.     }
  42.  
  43.     //std::cout << json.dump(4) << std::endl;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement