JSON c++14 中的数据处理 (Boost)

JSON data processing in c++14 (Boost)

我是 C++ 的新手(我大部分时间使用的是 embedded c)。我有这样的东西 JSON 数据:

[10, 
"Session" ,
{"currentYear":"2048","accidents":10,"status":"Accepted"}]

我正在尝试处理此数据:

boost::json::value testVal;
boost::json::error_code ec;
testVal = boost::json::parse(data, ec);

所以我在 testVal 中有多个项目,我需要从 testVal 获取对象来测试这样的东西:

boost::json::object testObj = testVal.get_object();
if(testObj.at("currentYear") == "2048")
{
// Do Something
}

但是我无法获取对象的实例,我正在寻找的数据在哪里。

知道如何检查(使用 boost::json)当前年份的数据是否为 ​​2048 吗?

感谢您的帮助。

直截了当的是

Compiler Explorer

for (auto& element : testVal.as_array()) {
    if (!element.is_object())
        continue;
    if (element.as_object()["currentYear"].as_string() == "2048") {
        std::cout << element << std::endl;
    }
}

尽管我建议使用图书馆中的转换工具。 (稍后会添加样本)

value_to

神奇的转换结果有点人为,因为您的输入...未指定。我假设您从数组中过滤对象,为了好玩,让我们将 Accepted 值不区分大小写和空格解析为某种枚举:

Godbolt

#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace json = boost::json;
using boost::adaptors::filtered;

struct Insurance {
    long   currentYear, accidents;
    enum class Status { Accepted, Rejected, Pending } status;

    friend Status tag_invoke(json::value_to_tag<Status>,
                                json::value const& v) {
        using namespace boost::spirit::x3;

        static const struct S : symbols<Status> {
            S() { this->add
                ("accepted", Status::Accepted)
                ("rejected", Status::Rejected)
                ("pending", Status::Pending);
            }
        } _sym;

        Status result;
        auto& str = v.as_string();

        if (not phrase_parse(str.begin(), str.end(), no_case[_sym] >> eoi,
                             space, result))
            throw std::domain_error("Status");
        return result;
    }
    friend Insurance tag_invoke(json::value_to_tag<Insurance>,
                                json::value const& v) {
        auto& o = v.as_object();
        return {
            std::atol(o.at("currentYear").as_string().data()),
            o.at("accidents").as_int64(),
            Status{}
        };
    }
};

json::value make_array(auto&& range) {
    return json::array(range.begin(), range.end());
}

int main() {
    auto data = boost::json::parse(R"(
        [10, 
        "Session",
        {"currentYear":"2048","accidents":10,"status":"Accepted"},
        {"currentYear":"2049","accidents":11,"status":" rejected"},
        {"currentYear":"2050","accidents":12,"status":"Accepted"},
        {"currentYear":"2051","accidents":13,"status":"pendinG"},
        12,
        "Session"
    ]
    )").as_array();

    auto objects = make_array(data | filtered(std::mem_fn(&json::value::is_object)));

    auto vec = json::value_to<std::vector<Insurance>>(objects);

    for (Insurance const& obj : vec) {
        std::cout << "Year: " << obj.currentYear
                  << " Accidents:" << obj.accidents << std::endl;
    }
}

版画

Year: 2048 Accidents:10
Year: 2049 Accidents:11
Year: 2050 Accidents:12
Year: 2051 Accidents:13