c++ - 如何在 C++ 中使用 boost-property-tree 从 JSON 文件中读取对象数组
问题描述
我在 C++ 应用程序中使用 boost-property-tree,我试图读取 JSON 文件users.json
并将数据存储到对象向量 ( std::vector<User> users;
) 中。
JSON 文件如下所示:
{
"OperatingSystem":"Windows 10",
"users" :
[
{
"firstName":"John",
"lastName":"Black"
},
{
"firstName":"Kate",
"lastName":"Red"
},
{
"firstName":"Robin",
"lastName":"White"
}
]
}
OperatingSystem
我已经使用以下代码行成功读取了该属性:
boost::property_tree::ptree treeRoot;
boost::property_tree::read_json("users.json", treeRoot);
std::string operatingSystem = treeRoot.get<std::string>("OperatingSystem");
std::cout << "OS : " << operatingSystem << std::endl;
这很好用。
为了存储用户,我创建了一个用户类。你可以看到下面的头文件User.hpp
:
#ifndef USER_H
#define USER_H
#include <iostream>
#include <string>
class User
{
private:
// Properties
std::string firstName;
std::string lastName;
public:
// Constructors
User();
User(std::string firstName, std::string lastName);
// Getters
std::string getFirstName();
std::string getLastName();
// Setters
void getFirstName(std::string firstName);
void getLastName(std::string lastName);
};
#endif // USER_H
和User.cpp
这里的文件:
#include "User.hpp"
#include <iostream>
#include <string>
// Constructors
User::User()
{
this->firstName = "";
this->lastName = "";
}
User::User(std::string firstName, std::string lastName)
{
this->firstName = firstName;
this->lastName = lastName;
}
// Getters
std::string User::getFirstName()
{
return firstName;
}
std::string User::getLastName()
{
return lastName;
}
// Setters
void User::getFirstName(std::string firstName)
{
this->firstName = firstName;
}
void User::getLastName(std::string lastName)
{
this->lastName = lastName;
}
在我的main.cpp
我试图将用户加载到一个向量中:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "User.hpp"
int main(int argc, char **argv)
{
boost::property_tree::ptree treeRoot;
boost::property_tree::read_json("users.json", treeRoot);
std::vector<User> users;
users = treeRoot.get<std::vector<User>>("users");
return 0;
}
但它不起作用。
如果有人知道如何通过 读取 JSON 文件中的对象数组boost::property_tree::ptree
,请告诉我。
解决方案
在property_tree
数组中,JSON 被映射到节点(称为 just ptree
)。什么是节点/ptree?
node/ptree {
data // has data
list < pair<key,node> > children // has children
}
在您的输入对象中,您的属性users具有值作为具有 3 个元素的数组。这些元素被映射到三个以空字符串为键的节点。
所以我们有:
"users" node:
children {"",nodeA}, {"",nodeB}, {"",nodeC}
nodeA,B,C
表示数组的元素。数组的每个元素都是具有 2 个属性的对象。像数组这样的对象也被映射到节点中。
所以nodeA
看起来像:
nodeA:
children {"firstName",nodeD},{"lastName",nodeE} \\ as children we have 2 properties of object
最后,nodeD
是
nodeD:
data = John
children emptyList
获取用户属性调用get_child()
。
要遍历所有子级,请使用返回的begin\end
方法。返回迭代器以作为键配对,并作为嵌套的 ptree 实例。ptree
get
begin\end
first
second
下面的代码遍历数组中的元素:
boost::property_tree::ptree pt;
boost::property_tree::read_json("in.json",pt);
auto it = pt.get_child("users");
for (auto it2 = it.begin(); it2 != it.end(); ++it2)
{
for (auto it3 = it2->second.begin(); it3 != it2->second.end(); ++it3)
{
std::cout << it3->first; // [1]
std::cout << " : " << it3->second.data() << std::endl;
}
// [2]
std::cout << std::endl;
}
并打印:
firstName : John
lastName : Black
firstName : Kate
lastName : Red
firstName : Robin
lastName : White
在 [1] 行中您应该存储firstName
/lastName
并且在 [2] 行中您可以创建新User
实例并推入向量。
推荐阅读
- python - 如何将 Python 中的输入限制为仅限日期?
- python - 将变量转换为 timedelta 但以天而不是时间为单位
- sqlite - 如何在 DB Browser for SQLite 的 DB Schema 侧栏中保持展开所有表设置?
- node.js - 如何使用 nodejs 下载 .xlsx 文件
- rotation - 如何对齐四元数数据?
- intellij-idea - Intellij 存储库差异 只读
- javascript - 如何检测 React 上的 Crtl + Shift + R 和浏览器重新加载按钮
- android - 从原生 Android 调用方法后 Flutter 应用程序重启而不是恢复
- laravel - 如果预订已满,如何制定条件
- dataverse - coe-starter-kit 是否提供 API 端点?