提交 ddcd23bd 编写于 作者: 栩xx's avatar 栩xx

初步实现json数据解析和输出

上级 13b8c4a7
...@@ -4,6 +4,5 @@ ...@@ -4,6 +4,5 @@
"\\221900107", "\\221900107",
"\\221900107\\src" "\\221900107\\src"
], ],
"SelectedNode": "\\221900107\\src\\data",
"PreviewInSolutionExplorer": false "PreviewInSolutionExplorer": false
} }
\ No newline at end of file
无法预览此类型文件
#include "Lib.h"
//入口:输入的文件名、输出的文件名
//返回值:无
//作用:初始化对象
OlympicProgram::OlympicProgram(string input, string output)
{
this->input = input;
this->output = output;
}
//入口:处理输入的文件
//返回值:无
//作用:根据所输入的信息调用不同的函数来处理
void OlympicProgram::resolveInput()
{
ifstream myfile(this->input);
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
return;
}
ofstream file_writer(this->output, ios_base::out);
while (getline(myfile, temp))
{
cout << temp << endl;
if (temp._Equal("total"))
{
output1();
}
else if (temp.substr(0,8)._Equal("schedule"))
{
string date = temp.substr(9, 4);
output2(date);
}
else
{
}
}
myfile.close();
}
//入口:无
//返回值:无
//作用:处理指令为total时的输出
void OlympicProgram::output1()
{
ifstream myfile("./data/total.json");
ofstream outfile(this->output, ios::app);
Json::Reader reader;
Json::Value root;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
return;
}
if (reader.parse(myfile, root))
{
int num = root["data"]["total"].asInt();
Json::Value node = root["data"]["medalsList"];
for (int i = 0; i < num; i++)
{
outfile << "rank" << (i + 1) << ':' << node[i]["countryname"].asString() << '\n';
outfile << "gold:" << node[i]["gold"].asString() << '\n';
outfile << "silver:" << node[i]["silver"].asString() << '\n';
outfile << "bronze:" << node[i]["bronze"].asString() << '\n';
outfile << "total:" << node[i]["count"].asString() << '\n';
outfile << "--------------------------------------------------" << '\n';
}
}
myfile.close();
outfile.close();
}
//入口:比赛日期
//返回值:无
//作用:处理指令为具体比赛日期时的输出
void OlympicProgram::output2(string date)
{
ifstream myfile("./data/"+date+".json");
ofstream outfile(this->output, ios::app);
Json::Reader reader;
Json::Value root;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
return;
}
if (reader.parse(myfile, root))
{
int num = root["data"]["total"].asInt();
Json::Value node = root["data"]["matchList"];
for (int i = 0; i < num; i++)
{
string time = node[i]["startdatecn"].asString().substr(11, 5);
outfile << "time:" << time << '\n';
outfile << "sport:" << node[i]["itemcodename"].asString() << '\n';
outfile << "name:" << node[i]["title"].asString() << '\n';
outfile << "venue:" << node[i]["venuename"].asString() << '\n';
outfile << "--------------------------------------------------" << '\n';
}
}
myfile.close();
outfile.close();
}
\ No newline at end of file
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include "include/json/json.h"
using namespace std;
//Ҫ
class OlympicProgram {
private:
string input;
string output;
public:
OlympicProgram(string input,string output);
void resolveInput();
void output1();
void output2(string date);
};
// OlympicSearch.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "Lib.h"
#pragma comment(lib, "lib_json.lib")
int main(int argc, char* argv[])
{
OlympicProgram test("input.txt", "output.txt");
test.resolveInput();
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册