boost::json comment extraire un attribut double d'un objet JSON - exemple minimal

extract_double_from_json.cpp
#include <boost/json.hpp>
#include <iostream>

int main() {
    // The JSON string to parse
    std::string json_str = "{\"timestamp\":1675910171.91}";

    // Parse the JSON string
    boost::json::value jv = boost::json::parse(json_str);

    // Extract the timestamp as a double
    double timestamp = jv.as_object()["timestamp"].as_double();

    // Output the result
    std::cout << "Timestamp: " << timestamp << std::endl;

    return 0;
}

Compilez comme ceci :

compile_jsontest.sh
g++ -o jsontest jsontest.cpp -lboost_json

Exemple de sortie

jsontest_output.txt
Timestamp: 1.67591e+09

Consultez les articles similaires par catégorie : Boost C/C++