读取和解析 JSON 文件 c++ BOOST
Read & Parse a JSON file c++ BOOST
由于已经有很多问题了,我有点担心问。。。但是
我看了很多不同的问题,但其中任何一个都不适合我。我尝试了以下代码,但它不起作用:
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
using namespace boost::property_tree;
...
std::ifstream jsonFile("test_file.json");
if (!jsonFile){
std::cerr << "Error opening filen";
return -1;
}
ptree pt;
json_parser::read_json(jsonFile, pt);
for (auto& array_element : pt) {
for (auto& property : array_element.second) {
std::cout << property.first << " = " << property.second.get_value<std::string>() << "n";
}
}
其内容采用以下格式:
[{"number": 1234,"string": "hello world"}, {"number": 5678,"string": "foo bar"}, ... etc }]
我无法让它先读出1234
,然后读出hello world
。事实上,它什么都没做。如何从我的.JSON
文件中读取?
我不完全确定问题是什么。它似乎有效(一旦您使JSON有效(:
UPDATE:增强JSON
Boost 1.75.0引入了Boost JSON,这是一种非常优越的实际处理JSON的方法:Live On Wandbox
#include <boost/json.hpp> #include <boost/json/src.hpp> #include <iostream> #include <iterator> #include <fstream> namespace json = boost::json; struct Rec { int64_t number; std::string string; friend Rec tag_invoke(json::value_to_tag<Rec>, json::value const& v) { auto& o = v.as_object(); return { o.at("number").as_int64(), value_to<std::string>(o.at("string")), }; } friend void tag_invoke(json::value_from_tag, json::value& v, Rec const& rec) { v = json::object{ {"number", rec.number}, {"string", rec.string}, }; } }; int main() { std::ifstream ifs("input.txt"); std::string input(std::istreambuf_iterator<char>(ifs), {}); using Recs = std::vector<Rec>; Recs recs = value_to<std::vector<Rec>>(json::parse(input)); for (auto& [n, s] : recs) { std::cout << "Rec { " << n << ", " << std::quoted(s) << " }n"; // some frivolous changes: n *= 2; reverse(begin(s), end(s)); } std::cout << "Modified json: " << json::value_from(recs) << "n"; }
打印
Rec { 1234, "hello world" } Rec { 5678, "foo bar" } Modified json: [{"number":2468,"string":"dlrow olleh"},{"number":11356,"string":"rab oof"}]
在Coliru上直播
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
int main() {
using boost::property_tree::ptree;
std::ifstream jsonFile("input.txt");
ptree pt;
read_json(jsonFile, pt);
for (auto & array_element: pt) {
for (auto & property: array_element.second) {
std::cout << property.first << " = " << property.second.get_value < std::string > () << "n";
}
}
}
input.txt
包含:
[{"number": 1234, "string": "hello world"},{"number": 5678, "string": "foo bar"}]
打印
number = 1234
string = hello world
number = 5678
string = foo bar
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送