Ed *_*hns 4 c++ json boost boost-propertytree
我想使用 Boost(属性树)库来解析以下有效的 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
}
] }
Run Code Online (Sandbox Code Playgroud)
我确实验证了 JSON 格式是正确的。
我还咨询了几个网站,例如:
但我仍然没有得到正确的结果。我想收集“用户”和“挑战”作为键/值对。最好的结果是将“挑战”(问题/答案)和用户信息(用户 ID、dateProperty)写入可以写入 std::map 的 std::pair 中。
任何建议,将不胜感激?
我想像往常一样,你只是对ptree 如何存储 JSON 数组感到困惑?
这是一个快速演示:
#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";
}
Run Code Online (Sandbox Code Playgroud)
印刷:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2666 次 |
| 最近记录: |