如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组

How can I read an array of object from a JSON file using boost-property-tree in C++

本文关键字:JSON 文件 属性 取对象 读取 数组 C++      更新时间:2023-10-16

我在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。