使用nlohmann获取并转换为struct of struct

问题描述 投票:0回答:1

尝试使用 Get 请求中的 nlohmann 并将其存储在结构中。结构体内部可以有结构体。我已经阅读了 nlohmanns 文档,但需要更简单的方法。预先感谢!

另请添加如何在 cmake 列表文件中添加库。

c++ nlohmann-json
1个回答
0
投票

像这样添加到cmake中-


cmake_minimum_required(VERSION 3.1)

project(cpp-interview-prep)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Boost)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
  add_executable(boosttest src/boosttest.cpp)
endif()

find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})

include(FetchContent)
FetchContent_Declare(
    nlohmann_json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.11.2  # Use the latest version or the one you need
)

FetchContent_MakeAvailable( nlohmann_json )

add_executable(hello src/helloworld.cpp)
target_link_libraries(hello PRIVATE ${CURL_LIBRARIES})
target_include_directories ( hello PUBLIC ${nlohmann_json_SOURCE_DIR}/include )

代码是这样的 -

#include<iostream>
#include <sstream>
#include<curl/curl.h>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

// Function to write the response data into a string
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::stringstream*)userp)->write((char*)contents, size * nmemb);
    return size * nmemb;
}

struct CountryProbablity {
  std::string country;
  double probability;
};

struct Person {
    int count;
    std::vector<CountryProbablity> country_probability;
    std::string name;
};

// Define how to convert from JSON to CountryProbablity
void from_json(const json& j, CountryProbablity& cp) {
    j.at("country_id").get_to(cp.country);
    j.at("probability").get_to(cp.probability);
}

// Define how to convert from JSON to Person
void from_json(const json& j, Person& p) {
    j.at("count").get_to(p.count);
    j.at("name").get_to(p.name);
    j.at("country").get_to(p.country_probability);
}

int main(int argc, char *argv[]) {
  CURL* curl;
  CURLcode res;
  std::stringstream response_stream;

  curl = curl_easy_init();

  const char *data = "foo=bar"; 
  curl_easy_setopt(curl, CURLOPT_URL, "https://api.nationalize.io/?name=Ankita");
  curl_easy_setopt(curl, CURLOPT_HTTPGET, true);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_stream);
  res = curl_easy_perform(curl);
  char* final_url;
  curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &final_url);
  std::cout << "Final URL: " << final_url << std::endl;
  if(res == CURLE_OK) {
    long response_code;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
    std::cout << "Got response code: " << response_code << std::endl;
    json json_response = json::parse(response_stream.str());
    Person person;
    from_json(json_response, person);
    std::cout << person.count << " " << person.name << " " << person.country_probability.size() << std::endl;  
  } else {
    std::cout << "Failure: " << curl_easy_strerror(res);
  }

  curl_easy_cleanup(curl);
  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.