Ori*_*er7 1 c++ json boost boost-propertytree
我在 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我已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;
Run Code Online (Sandbox Code Playgroud)
效果很好。
为了存储用户,我创建了一个 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
Run Code Online (Sandbox Code Playgroud)
以及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;
}
Run Code Online (Sandbox Code Playgroud)
在我的例子中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;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
如果有人知道如何通过 读取 JSON 文件中的对象数组boost::property_tree::ptree,请告诉我。
在property_tree数组中,JSON 被映射到节点(称为ptree)。什么是节点/ptree?
node/ptree {
data // has data
list < pair<key,node> > children // has children
}
Run Code Online (Sandbox Code Playgroud)
在您的输入对象中,您有属性users,其值为包含 3 个元素的数组。这些元素被映射到以空字符串为键的三个节点。
所以我们有:
"users" node:
children {"",nodeA}, {"",nodeB}, {"",nodeC}
Run Code Online (Sandbox Code Playgroud)
nodeA,B,C表示数组的元素。数组的每个元素都是具有 2 个属性的对象。像数组这样的对象也被映射到节点。
所以nodeA看起来像:
nodeA:
children {"firstName",nodeD},{"lastName",nodeE} \\ as children we have 2 properties of object
Run Code Online (Sandbox Code Playgroud)
最后nodeD是
nodeD:
data = John
children emptyList
Run Code Online (Sandbox Code Playgroud)
获取用户属性调用get_child()。
要迭代所有子项,请使用返回的begin\end方法。返回迭代器作为键和嵌套 ptree 实例配对。ptreegetbegin\endfirstsecond
下面的代码迭代数组中的元素:
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;
}
Run Code Online (Sandbox Code Playgroud)
并打印:
firstName : John
lastName : Black
firstName : Kate
lastName : Red
firstName : Robin
lastName : White
Run Code Online (Sandbox Code Playgroud)
在 [1] 行中,您应该存储firstName/ lastName,在 [2] 行中,您可以创建新User实例并推入向量。