c++ - 读取和解析 JSON 文件 C++ BOOST

标签 c++ json boost boost-propertytree

因为已经有很多关于这个的问题,我有点担心问...但是

我看过许多不同的问题,但没有任何问题对我有用。我有此代码作为我的尝试,但它不起作用:

#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 file\n";
    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: Boost JSON

Boost 1.75.0 introduced Boost JSON, far superior way to actually deal with 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";
}

Printing

Rec { 1234, "hello world" }
Rec { 5678, "foo bar" }
Modified json: [{"number":2468,"string":"dlrow olleh"},{"number":11356,"string":"rab oof"}]

Live On 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

关于c++ - 读取和解析 JSON 文件 C++ BOOST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251469/

相关文章:

c++ - 将 QNetworkReply 写入文件

JavaScript、JSON、PhoneGap

php - 在 php laravel 中将 html 转换为 json

c++ - 如何更新 boost multi_index_container 的值?

c++ - Boost::Python 绑定(bind)类继承

c++ - OSX 上的 CMake 和 Boost - 没有制定目标的规则

c++ - 链表字符串排序问题

c++ - 检查包含的库 C++ 的大小

c++ - 如何在 DirectX11 中正确创建聚光灯?

asp.net - WCF Restful Web 服务返回 JSON