what():没有这样的节点json boost

hec6srdp  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(212)

我正在尝试使用boost::property_tree解析以下JSON文件。

{
    "pi": 3.141,
    "temp": 3.141,
    "happy": true,
    "name": "Niels",
    "nothing": null,
    "answer": {
        "everything": 42
    },
    "list": [
        1,
        0,
        2
    ],
    "object": {
        "currency": "USD",
        "value": 42.99
    }
}

但对于第二个节点“temp”,它给出错误-

terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::ptree_bad_path>'
  what():  No such node (temp)
Aborted (core dumped)
@ubuntu:~/git_

编写了如下代码-

std::ifstream file(jsonPath, std::ifstream::binary);
  using json = nlohmann::json;
  namespace pt = boost::property_tree;
  pt::ptree root;
  pt::read_json(jsonPath, root);  // Load the json file in this ptree 
  string valPi = root.get<string>("pi");  
  string valtemp = root.get<string>("temp");
neskvpey

neskvpey1#

Boost属性树不是JSON库。
如果已经包含了nlohmann::json,为什么还要使用它呢?
你的选择器路径可能与JSON不匹配,这很难说,因为你展示的例子工作正常:

**一个

图纸

{
    "pi": "3.141",
    "temp": "3.141",
    "happy": "true",
    "name": "Niels",
    "nothing": "null",
    "answer": {
        "everything": "42"
    },
    "list": [
        "1",
        "0",
        "2"
    ],
    "object": {
        "currency": "USD",
        "value": "42.99"
    }
}
Yay 3.141 and 3.141

没问题。
但是,请注意所有类型信息是如何丢失的,这只是触及PropertyTree中“限制”的表面:https://www.boost.org/doc/libs/1_75_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser

使用Boost JSON

严肃点:

#include <boost/json.hpp>
#include <iostream>

int main() {
    auto root = boost::json::parse(R"({
    "pi": 3.141,
    "temp": 3.141,
    "happy": true,
    "name": "Niels",
    "nothing": null,
    "answer": {
        "everything": 42
    },
    "list": [
        1,
        0,
        2
    ],
    "object": {
        "currency": "USD",
        "value": 42.99
    }
})");

    std::cout << root << "\n";
    auto valPi   = root.at("pi").get_double();
    auto valtemp = root.at("temp").get_double();

    std::cout << "Yay " << valPi << " and " << valtemp << "\n";
}

图纸

{"pi":3.141E0,"temp":3.141E0,"happy":true,"name":"Niels","nothing":null,"answer":{"everything":42},"list":
[1,0,2],"object":{"currency":"USD","value":4.299E1}}
Yay 3.141 and 3.141

使用nlohmann::json

一个月一次

#include <nlohmann/json.hpp>
#include <iostream>

int main() {
    auto root = nlohmann::json::parse(R"({
    "pi": 3.141,
    "temp": 3.141,
    "happy": true,
    "name": "Niels",
    "nothing": null,
    "answer": {
        "everything": 42
    },
    "list": [
        1,
        0,
        2
    ],
    "object": {
        "currency": "USD",
        "value": 42.99
    }
})");

    std::cout << root << "\n";
    auto valPi   = root["pi"].get<double>();
    auto valtemp = root["temp"].get<double>();

    std::cout << "Yay " << valPi << " and " << valtemp << "\n";
}

图纸

{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency"
:"USD","value":42.99},"pi":3.141,"temp":3.141}
Yay 3.141 and 3.141
hmmo2u0o

hmmo2u0o2#

string valPi = root.get<string>("pi");

应将其更改为:

std::string valPi =root.get<std::string>(pt::ptree::path_type{"pi"});

如果您想要第2级:

int answer_everything =root.get<int>(pt::ptree::path_type{"answer.everthing"});

或者更喜欢使用另一种定界符:

int answer_everything =root.get<int>(pt::ptree::path_type{"answer\neverthing","\n"});

相关问题