을 받고 예외를 읽을 때 사용하여 값을 BOOST_FOREACH 에서 JSON 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.

할 수 있는 사람이 있었는 방법을 읽은 값 배열에서와 함께 아래 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

최고의 응답

1

당신은 질문에 매우 유사한 질문에 어제입니다. 우리는 당신에게 말을 남용하지 않는 재산 트리에 라이브러리 분석 JSON. 심지어는 예기:

더 많은 심각한 코드를 사용할 수 있습 유형 매핑

여기에는 방법 당신은에서 확장하는 대답을 분석하는 전체 배열로는 벡터 번에

라이브에 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 회 없는 구조체/벡터: 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ý
..................................................................................................................