未经处理的例外时,试图检索的价值,从id ptree提高使用C++

0

的问题

我得到以下的错误,当阅读的价值从id ptree提高使用C++

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

有形之间的不匹配你的代码和数据:

  • 数据是一个普通的嵌套的字典: Student.name 是"约翰".
  • 代码希望看到一系列下 Student 钥,因此,它试图取 Student.0.name, Student.1.name,...为每一子项目的 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

首先,我可以建议的现代化和由此简化了你的代码,同时避免 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";
}

看到它 的生活

真正的 问题是:

使用JSON库

提高产树是 JSON图书馆。

提高JSON存在:

生活在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ý
..................................................................................................................