จะเป็นข้อยกเว้นตอนที่อ่านค่าใช้ BOOST_FOREACH จาก LANGUAGE อาเรย์ในภาษา C++language

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.

เป็นไปได้มั้ยที่ใครบางคนจะได้ช่วยฉันยังต้องอ่านค่าต่างๆจากอาเรย์กับด้านล่าง LANGUAGE รูปแบบ?

#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

คุณขอให้เป็นคำถามที่คล้ายกันเมื่อวานนี้ เราบอกคุณแล้วว่าไม่ต้องการใช้เป็นบ้านต้นไม้ของสมุดเพื่อการวิเคราะห์ LANGUAGE. ฉันคาด:

สำหรับจริงจังมากกฎคุณอาจจะต้องใช้ประเภท-วางแผน

นี่คือวิธีที่คุณจะขยายจากนั้นคำตอบต้องการวิเคราะห์ทั้งหมดอาเรย์เข้าไปในเว็กเตอตอนครั้ง:

อยู่ 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

ในกรณีที่คุณชอบป้อนเอง LANGUAGE traversal โดยไม่มี struct/y: coliru.stacked-crooked.com/a/af3ddd1dac30cbd5
sehe

สวัสดี@sehe ฉันใช้ read_json กับ property_tree. คุณสามารถช่วยผมทำจะมีรูเล็กๆอยู่ใต้ปุ่ม-รูเล็กๆ LANGUAGE traversal ใช้ read_json และ property_tree(ที่อยู่ของฉันตัวอย่าง).
sas

แน่นอน coliru.stacked-crooked.com/a/d444f8aaf6c611f5 (เป็นเหมือนกับมันอยู่ในของฉัน แก่กว่าคำตอบ). อย่าทำอย่างนี้นะ และอ่านขึ้นไปบนขีดจำกัดของ"LANGUAGE"ปรับแต่งโปรแกรมเบื้องหลังในเพิ่มทรัพย์สินผังต้นไม้: 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่