Json what():没有这样的节点
我正在尝试使用boost::property\u树解析下面的JSON文件。Json what():没有这样的节点,json,boost,boost-propertytree,Json,Boost,Boost Propertytree,我正在尝试使用boost::property\u树解析下面的JSON文件。 { "pi": 3.141, "temp": 3.141, "happy": true, "name": "Niels", "nothing": null, "answer": { "everything&qu
{
"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");
terminate在抛出'boost::wrapexcept'实例后调用
what():没有这样的节点(temp)
中止(堆芯转储)
@ubuntu:~/git_
编写了如下代码-
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");
std::ifstream文件(jsonPath,std::ifstream::binary);
使用json=nlohmann::json;
名称空间pt=boost::属性树;
pt::ptree根;
pt::read_json(jsonPath,root);//在此ptree中加载json文件
字符串valPi=root.get(“pi”);
字符串valtemp=root.get(“temp”);
Boost属性树不是JSON库
如果已经包含了nlohmann::json
,为什么要(ab)使用它
您的选择器路径可能与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";
}
印刷品
{
"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
{"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
{"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
没问题
但是,请注意,所有类型信息是如何丢失的,这只是触及PropertyTree中“限制”的表面:
使用boostjson
严肃点:
#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";
}
使用
#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";
}
您不应该使用
root.get
?