如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
How can I read an array of object from a JSON file using boost-property-tree in C++
我在C++应用程序中使用boost-property-tree,我试图users.json
读取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 类。您可以在下面看到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;
}
但它不起作用。
如果有人知道如何通过boost::property_tree::ptree
从 JSON 文件中读取对象数组,请告诉我。
在property_tree
数组中,JSON 被映射到节点(仅称为ptree
(。 什么是节点/ptree?
node/ptree {
data // has data
list < pair<key,node> > children // has children
}
在您的输入对象中,您有属性用户,其值作为包含 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()
。
要遍历所有子项,请在get
返回的ptree
上使用beginend
方法。beginend
返回迭代器以配对first
作为键,second
作为嵌套的 ptree 实例。
下面的代码遍历数组中的元素:
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
实例并推送到 Vector。
相关文章:
- 如何在特定位置追加 json 文件
- 如何在Visual C+++中从JSON文件中读取节点数据
- 如何将 json 数据附加到 Qt 中的现有 json 文件中
- 当前步骤:构建任务.json 文件
- 可以在JSON文件的帮助下在Qt中创建UI表单
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 有没有办法从Makefile访问VS Code c_cpp_properties.json文件中的定义
- 保存json文件后如何返回文件路径
- 构造一个类似于JSON文件c++的字符串
- 使用 jsoncpp 读取 JSON 文件
- 在C++中打开 json 文件时遇到问题
- Nlohmann 在不知道密钥的情况下解析 JSON 文件
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- 如何使用QJSONARRAY在QT5(C )中解析JSON文件(数组)
- 使用RapidJson凝结JSON文件后,如何将输出存储在C 字符串中
- C++ 使用 json 文件提升程序选项
- 如何将值数组从 json 文件读取到 c++ 数组
- 在大型 JSON 文件中查找文本
- FlatBuffers:未支撑的工会向量错误将JSON文件转换为二进制文件
- 如何将JSON文件中的数据转换为值(double,int ..)