得到一个异常的时候读的价值观使用BOOST_FOREACH从id列在C++

0

的问题

我得到以下的错误,当阅读的价值观使用BOOST_FOREACH:

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

可能有人帮助我怎么读取数值列有以下关系图来描述软件所需的依赖?

#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

最好的答案

1

你问了一个非常类似的问题,昨天。 我们告诉过你不要滥用财产树木图书馆分析手机中。 我甚至预期:

更严重的代码你可能想使用类型映射

这是你如何扩大从这一回答分析整个列入一个向量在一次:

生活在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";
}

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

我甚至扔在一个方便的调试行的情况下,你需要帮助找出到底是什么你会得到在某些时点:

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

如果你喜欢手册JSON历没有struct/矢量: coliru.stacked-crooked.com/a/af3ddd1dac30cbd5
sehe

嗨@东,我使用read_json与property_tree. 你可以帮我如何做到的手册JSON历使用read_json和property_tree(因为它是在我的实例)。
sas

肯定的。 coliru.stacked-crooked.com/a/d444f8aaf6c611f5 (基本上是相同的,因为它是在我的 旧的答复). 不这样做,虽然。 和阅读有关的限制"JSON"后端在提高产树: boost.org/doc/libs/1_77_0/doc/html/property_tree/...
sehe

其他语言

此页面有其他语言版本

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