首页 > 解决方案 > 如何在 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,请告诉我。

标签: c++jsonboostptree

解决方案


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 实例。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;
    }

并打印:

firstName : John
lastName : Black

firstName : Kate
lastName : Red

firstName : Robin
lastName : White

在 [1] 行中您应该存储firstName/lastName并且在 [2] 行中您可以创建新User实例并推入向量。


推荐阅读