#include <iostream> #include <fstream> #include<string> #include "nlohmann/json.hpp" using json = nlohmann::json; // 定义链表节点 struct Node { int comment; std::string from; std::vector<std::string> comments; std::string to; float score; std::string corp; std::string time; Node* next; }; void readJsonToLinkedList(std::string filename, Node*& head) { std::ifstream infile(filename); json j; infile >> j; for (auto& element : j) { Node* newNode = new Node(); newNode->comment = element["comment"]; newNode->from = element["from"]; newNode->comments = element["comments"]; newNode->to = element["to"]; newNode->score = std::stof(element["score"].get<std::string>()); newNode->corp = element["corp"]; newNode->time = element["time"]; // 插入节点到链表尾部 if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } } //从链表中进行筛选的函数 void saveRecords(Node* head, float lowerBound, float upperBound) { std::ofstream outfile("results.dat"); Node* current = head; while (current != nullptr) { if (current->score >= lowerBound && current->score <= upperBound) { outfile << current->from << "," << current->to << "," << current->score << std::endl; } current = current->next; } outfile.close(); } int main() { Node* head = nullptr; readJsonToLinkedList("C:\\Users\\86130\\source\\repos\\数据结构课程设计\\rating.json", head); // 从用户输入中获取评分范围 float low, high; std::cout << "请输入评分下界: "; std::cin >> low; std::cout << "请输入评分上界:"; std::cin >> high; saveRecords(head, low, high); return 0; }对于该代码是如何进行数据处理的
时间: 2024-03-07 13:51:03 浏览: 22
该代码是一个 C++ 程序,它通过读取一个 JSON 格式的文件并将其转换为链表的形式,然后根据用户输入的评分范围筛选链表中的节点数据,并将结果保存到一个名为 "results.dat" 的文件中。
具体来说,该程序首先定义了一个链表节点 Node,其中包含了评分(comment)、评分者(from)、被评分者(to)、评论(comments)、评分时间(time)、评分公司(corp)和得分(score)七个属性,以及一个指向下一个节点的指针 next。
然后,程序定义了一个函数 readJsonToLinkedList,用于将 JSON 文件中的数据读入到链表中。该函数首先打开指定的文件,然后使用 nlohmann/json 库对文件内容进行解析,解析后的数据存储在一个名为 j 的变量中。接着,程序遍历 j 中的每一个元素,即每一条评分记录,为它创建一个新的链表节点,并将该节点插入到链表的尾部。
最后,程序定义了一个名为 saveRecords 的函数,用于从链表中筛选出评分在指定范围内的节点,并将它们的评分者、被评分者和得分保存到一个名为 "results.dat" 的文件中。
在 main 函数中,程序首先定义了一个头指针 head,并将其初始化为 nullptr。然后,程序调用 readJsonToLinkedList 函数,将指定的 JSON 文件读入到链表中。接着,程序提示用户输入评分范围,然后调用 saveRecords 函数,将符合条件的节点数据保存到文件中。最后,程序返回 0,表示程序正常结束。
相关问题
nlohmann_json读取一个json文件中的结构体数组
要读取一个 JSON 文件中的结构体数组,我们需要定义一个结构体,并使用 `nlohmann_json` 库提供的 `json::parse()` 函数来解析 JSON 文件。以下是一个示例代码:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// 定义一个结构体
struct Person {
std::string name;
int age;
};
int main() {
// 打开 JSON 文件
std::ifstream ifs("example.json");
// 读取 JSON 文件
json j = json::parse(ifs);
// 解析 JSON 数组
std::vector<Person> persons;
for (auto& elem : j["data"]) {
Person p;
p.name = elem["name"];
p.age = elem["age"];
persons.push_back(p);
}
// 遍历结构体数组
for (auto& p : persons) {
std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl;
}
return 0;
}
```
在这个示例中,我们首先定义了一个名为 `Person` 的结构体,它包含了一个名为 `name` 的字符串和一个名为 `age` 的整数。然后我们打开了一个名为 `example.json` 的 JSON 文件,使用 `json::parse()` 函数将其解析成一个 `json` 对象。由于这个 JSON 对象中含有一个名为 `data` 的结构体数组,我们可以使用 `j["data"]` 来访问它,并使用 for 循环遍历其中的元素,将每个元素解析为一个结构体,并将其添加到一个 `std::vector<Person>` 中。
最后,我们再次使用 for 循环遍历结构体数组,并将每个结构体的 `name` 和 `age` 输出到控制台上。
需要注意的是,在解析 JSON 数组时,我们需要使用 for 循环遍历其中的元素,并将每个元素解析为一个结构体。在解析时,我们可以使用 `elem["name"]` 和 `elem["age"]` 来访问结构体中的字段。
c++ json转map
你可以使用第三方库如 JSON for Modern C++ 来将 JSON 转换成 C++ 中的 std::map。下面是一个简单的示例代码:
```c++
#include <iostream>
#include <fstream>
#include <map>
#include "json.hpp"
using json = nlohmann::json;
int main() {
// 从文件中读取 JSON 数据
std::ifstream i("data.json");
json j;
i >> j;
// 将 JSON 转换成 std::map
std::map<std::string, std::string> my_map = j.get<std::map<std::string, std::string>>();
// 输出转换后的 std::map
for(auto const& pair: my_map) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
```
其中,`data.json` 文件中包含了一个 JSON 对象,例如:
```json
{
"name": "John",
"age": "30",
"city": "New York"
}
```
在上述代码中,我们首先从文件中读取了 JSON 数据,然后使用 `get()` 函数将其转换为 `std::map<std::string, std::string>` 类型,并最终输出了转换后的 `my_map` 对象。