解析JSON文件(C++ Boost)

5

我想使用Boost (Property Tree)库来解析以下有效的JSON文件:

{
    "user": {
        "userID": "5C118C8D-AA65-49C0-B907-348DE87D6665",
        "dateProperty": "05-06-2015"
    },
    "challenges": [
        {
            "question#1": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        },
        {
            "question": "answer",
            "value": 5
        }
    ] }

我已经确认JSON格式正确。

我还咨询了几个网站,例如:

但是我仍然没有得到正确的结果。我想将“user”和“challenges”作为键值对收集起来。最好的结果是将“challenges”(问题/答案)和用户信息(userID,dateProperty)写入std::pair中,然后可以将其写入std:map。

任何建议都会受到赞赏。

1个回答

6

更新:自Boost 1.75起,建议使用Boost JSON,请参见以下内容。

我认为你只是像往常一样对于 ptree 如何存储 JSON 数组感到困惑了吗?

这里有一个快速演示:

在 Coliru 上实时运行

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <fstream>

int main()
{
    using namespace boost::property_tree;

    ptree pt;
    read_json(std::cin, pt);

    for (auto& challenge : pt.get_child("challenges"))
        for (auto& prop : challenge.second)
            std::cout << prop.first << ": " << prop.second.get_value<std::string>() << "\n";
}

输出:

question#1: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5
question: answer
value: 5

更新:使用Boost JSON更好

点击此处查看演示效果。

#include <boost/json.hpp>
#include <boost/json/src.hpp> // for header only
#include <iostream>

int main() {
    std::string const input(std::istreambuf_iterator<char>(std::cin), {});
    boost::json::error_code ec;
    auto doc = boost::json::parse(input, ec);

    for (auto& challenge : doc.at("challenges").as_array()) {
        std::cout << " --- " << challenge << "\n";
        for (auto& [k,v] : challenge.as_object())
            std::cout << k << ": " << v << "\n";
    }
}

打印

 --- {"question#1":"answer","value":5}
question#1: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5
 --- {"question":"answer","value":5}
question: "answer"
value: 5

自Boost 1.75起,更倾向于使用Boost JSON,请参见答案中的更新。 - sehe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接