Unhandled นข้อยกเว้นตอนที่พยายามที่จะดึงข้อมูค่าจาก LANGUAGE ptree ใช้เพิ่มภาษา C++language

0

คำถาม

ฉันเริ่มที่ด้านล่างนี้เกิดข้อผิดพลาดเมื่อมีการอ่านค่าจาก LANGUAGE ptree ใช้เพิ่มภาษา C++language

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

ด้านล่างนี้เป็นโปรแกรมเป็นไปได้มั้ยที่ใครบางคนจะได้โปรดช่วยฉันด้วยสิ่งที่ฉันเป็นหายอยู่ที่นี่

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

using namespace std;

using boost::property_tree::ptree;

int main()
{
    const char* f_strSetting = "{\"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("Student"))
    {
        std::string l_strColor;
        std::string l_strPattern;
        l_strColor = v.second.get <std::string>("Name");
        l_strPattern = v.second.get <std::string>("Course");
    }
    return 0;
}
boost boost-propertytree c++ json
2021-11-22 13:39:05
2

คำตอบที่ดีที่สุด

1

มันมีรูปร่าง mismatch ระหว่างรหัสของคุณและข้อมูลของคุณ:

  • ข้อมูลเป็นรอบการธรรมดาพจนานุกรม: Student.name คือ"จอห์น".
  • รหัสหวังว่าจะเห็นอาเรย์ใต้ Student กุญแจแล้วมันพยายามจะไปเอามา Student.0.name, Student.1.name,...สำหรับทุก subitem ของ Student.

จะแก้ไขรหัส:

// Drop the BOOST_FOREACH
auto & l_Student = pt1.get_child("Student");
l_strColor = l_Student.get<std::string>("Name");

หรือแก้ไขข้อมูล:

// Note the extra []
const char * f_strSetting = R"({"Student": [{"Name":"John","Course":"C++"}]})";
2021-11-22 14:03:27

ขอบคุณ @Botje
sas
1

อย่างแรกเลยก็คือผมขอแนะนำให้ modernizing และทำให้ simplifying ของคุณรหัสขณะที่หลีกเลี่ยง using เป้าหมาย:

#include <boost/property_tree/json_parser.hpp>
#include <string>
using boost::property_tree::ptree;

int main() {
    ptree pt;
    {
        std::istringstream l_issJson( R"({"Student": {"Name":"John","Course":"C++"}})");
        read_json(l_issJson, pt);
    }

    for(auto& [k,v] : pt.get_child("Student")) {
        auto name   = v.get<std::string>("Name");
        auto course = v.get<std::string>("Course");
    }
}

อย่างที่สองคุณเป็นการเลือกคนผิดระดับ-เป็นอีกคำตอบคะแนนออกไป:

#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <string>
using boost::property_tree::ptree;

int main() {
    ptree pt;
    {
        std::istringstream l_issJson( R"({"Student": {"Name":"John","Course":"C++"}})");
        read_json(l_issJson, pt);
    }

    auto name   = pt.get<std::string>("Student.Name");
    auto course = pt.get<std::string>("Student.Course");

    std::cout << "Name: '" << name << "', Course: '" << course << "'\n";
}

เห็นมัน อยู่

แต่ที่ จริง ปัญหาคือ:

ใช้ LANGUAGE องสมุด

เพิ่มทรัพย์สินผังต้นไม้คือ ไม่ได้ เป็น LANGUAGE องสมุด

เพิ่ม LANGUAGE มีอยู่:

อยู่ Coliru

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

int main() {
    auto pt = json::parse(R"({"Student": {"Name":"John","Course":"C++"}})");

    auto& student = pt.at("Student");
    auto  name    = student.at("Name").as_string();
    auto  course  = student.at("Course").as_string();

    std::cout << "Name: " << name << ", Course: " << course << "\n";
}

รอยนิ้วมือ

Name: "John", Course: "C++"

โบนัส

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

#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) {
        return {
            json::value_to<std::string>(v.at("Name")),
            json::value_to<std::string>(v.at("Course")),
        };
    }
};

int main()
{
    auto doc = json::parse(R"({"Student": {"Name":"John","Course":"C++"}})");
    auto s   = value_to<Student>(doc.at("Student"));

    std::cout << "Name: " << s.name << ", Course: " << s.course << "\n";
}
    

เห็นมัน อยู่ Coliru

2021-11-22 22:33:37

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

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

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