Ottenere un'eccezione quando la lettura di valori utilizzando BOOST_FOREACH dal JSON array in C++

0

Domanda

Ricevo il seguente errore durante la lettura dei valori utilizzando BOOST_FOREACH:

Unhandled exception at 0x76FCB502 in JSONSampleApp.exe: Microsoft C++ exception: boost::wrapexcept<boost::property_tree::ptree_bad_path> at memory location 0x00CFEB18.

Qualcuno potrebbe aiutarmi a capire come leggere i valori dell'array con il seguente formato JSON?

#include <string>
#include <iostream>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>

using namespace std;

using boost::property_tree::ptree;

int main()
{
const char* f_strSetting = R"({"Class": [{"Student": {"Name":"John","Course":"C++"}}]})";

    boost::property_tree::ptree pt1;
    std::istringstream l_issJson(f_strSetting);
    boost::property_tree::read_json(l_issJson, pt1);

    BOOST_FOREACH(boost::property_tree::ptree::value_type & v, pt1.get_child("Class.Student"))
    {
        std::string l_strName;
        std::string l_strCourse;
        l_strName = v.second.get <std::string>("Name");
        l_strCourse = v.second.get <std::string>("Course");

        cout << l_strName << "\n";
        cout << l_strCourse << "\n";
    }

    return 0;
}
boost boost-propertytree c++ json
2021-11-23 13:32:29
1

Migliore risposta

1

Ti ha fatto una domanda molto simile a ieri. Vi abbiamo detto di non abusare di una struttura di proprietà della biblioteca per il parsing JSON. Ho anche anticipato:

Per di più serio di codice si potrebbe desiderare di utilizzare il tipo di mappatura

Ecco come si sarebbe ampliata da quella risposta per analizzare l'intera matrice in un vettore in una volta:

In Diretta Su Coliru

#include <boost/json.hpp>
#include <boost/json/src.hpp> // for header-only
#include <iostream>
#include <string>
namespace json = boost::json;

struct Student {
    std::string name, course;

    friend Student tag_invoke(json::value_to_tag<Student>, json::value const& v) {
        std::cerr << "DEBUG: " << v << "\n";
        auto const& s = v.at("Student");
        return {
            value_to<std::string>(s.at("Name")),
            value_to<std::string>(s.at("Course")),
        };
    }
};

using Class = std::vector<Student>;

int main()
{
    auto doc = json::parse(R"({ "Class": [
            { "Student": { "Name": "John", "Course": "C++" } },
            { "Student": { "Name": "Carla", "Course": "Cobol" } }
        ]
    })");
    auto c = value_to<Class>(doc.at("Class"));

    for (Student const& s : c)
        std::cout << "Name: " << s.name << ", Course: " << s.course << "\n";
}

Stampa

Name: John, Course: C++
Name: Carla, Course: Cobol

Ho anche buttato in un pratico riga di debug in caso avete bisogno di aiuto per capire esattamente quello che si ottiene a un certo punto:

DEBUG: {"Student":{"Name":"John","Course":"C++"}}
DEBUG: {"Student":{"Name":"Carla","Course":"Cobol"}}
2021-11-23 15:05:58

Nel caso In cui si preferisce manuale JSON attraversamento senza struct/vettore: coliru.stacked-crooked.com/a/af3ddd1dac30cbd5
sehe

Ciao @sehe, sto usando read_json con property_tree. Mi potete aiutare su come fare il manuale JSON attraversamento utilizzando read_json e property_tree ( come nel mio esempio).
sas

Certo. coliru.stacked-crooked.com/a/d444f8aaf6c611f5 (fondamentalmente lo stesso come lo era nella mia precedente risposta). Non fare questo, però. E leggere sui limiti di "JSON" backend in Boost struttura di Proprietà: boost.org/doc/libs/1_77_0/doc/html/property_tree/...
sehe

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................