【问题标题】:Is there any way to add parameter from json file to vector<ObjClass>?有没有办法将 json 文件中的参数添加到 vector<ObjClass>?
【发布时间】:2019-09-13 12:27:20
【问题描述】:

我有一个类和文件“person.json”。有没有办法将该文件读取到像矢量这样的数据结构? 我的文件:

{
 "students": [
{
  "name": "Jack",
  "year": 1,
  "grade": 6.95
},
{
  "name": "Paul",
  "year": 2,
  "grade": 8.54
},
{
  "name": "John",
  "year": 3,
  "grade": 9.49
},
{
  "name": "Annie",
  "year": 1,
  "grade": 3.12
}
]
} 

我想从该文件中设置人名、人年和人等级,不像我的主班那样 我的主要课程:

int main() {

ifstream datafile("people.json");
json j;
datafile >> j;
cout << j << endl;
datafile.close();

vector<PersonClass> list;

string name = "Jonas";
float year = 420;
float grade = 69;
PersonClass *f1;
    f1 = new PersonClass;
    f1->set_name(name);
    f1->set_year(year);
    f1->set_grade(grade);

    list.push_back(*f1);
}

【问题讨论】:

标签: c++ json object vector


【解决方案1】:

正如 PaulMcKenzie 在 cmets 中提到的,获取一个面向 C++ 的 JSON 库。 jsonconsnlohmannThorsSerializer 都支持 JSON 和 C++ 数据结构之间的转换,请参阅 How To Convert Vector To Json Object? C++C++ JSON Serialization。关于您的具体问题,以下是在 jsoncons 中执行此操作的非侵入式方式。

#include <jsoncons/json.hpp>

class Person
{
    std::string name_;
    size_t year_;
    double grade_;
public:
    const std::string& get_name() const
    {
        return name_;
    }
    void set_name(const std::string& value)
    {
        name_ = value;
    }
    size_t get_year() const
    {
        return year_;
    }
    void set_year(size_t value)
    {
        year_ = value;
    }
    double get_grade() const
    {
        return grade_;
    }
    void set_grade(double value)
    {
        grade_ = value;
    }
};

class Students
{
    std::vector<Person> students_;
public:
    Students(const std::vector<Person>& students)
    {
        students_ = students;
    }
    const std::vector<Person>& get_students() const { return students_; }
};

JSONCONS_ALL_GETTER_SETTER_NAME_TRAITS(Person, (get_name,set_name,"name"), 
                                                (get_year,set_year, "year"), 
                                                (get_grade, set_grade, "grade"))
JSONCONS_ALL_CTOR_GETTER_NAME_TRAITS(Students, (get_students, "students"))

const std::string input = R"(
{
    "students": [
        {
            "name": "Jack",
            "year" : 1,
            "grade" : 6.95
        },
        {
          "name": "Paul",
          "year" : 2,
          "grade" : 8.54
        },
        {
          "name": "John",
          "year" : 3,
          "grade" : 9.49
        },
        {
          "name": "Annie",
          "year" : 1,
          "grade" : 3.12
        }
    ]
}
)";

int main()
{
    std::istringstream is(input);

    Students result = jsoncons::decode_json<Students>(is);

    std::cout << "(1)\n";
    for (const auto& person : result.get_students())
    {
        std::cout << person.get_name() << ", " << person.get_year() << ", " << person.get_grade() << "\n";
    }
    std::cout << "\n";

    std::ostringstream os;
    jsoncons::encode_json(result, os, jsoncons::indenting::indent);

    std::cout << "(2)\n";
    std::cout << os.str() << "\n";
}

输出:

(1)
Jack, 1, 6.95
Paul, 2, 8.54
John, 3, 9.49
Annie, 1, 3.12

(2)
{
    "students": [
        {
            "grade": 6.95,
            "name": "Jack",
            "year": 1
        },
        {
            "grade": 8.54,
            "name": "Paul",
            "year": 2
        },
        {
            "grade": 9.49,
            "name": "John",
            "year": 3
        },
        {
            "grade": 3.12,
            "name": "Annie",
            "year": 1
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 2020-09-28
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2021-07-11
    相关资源
    最近更新 更多