Compare commits
2 Commits
feature-xm
...
feature-js
| Author | SHA1 | Date | |
|---|---|---|---|
| d757710f5d | |||
| 54f612e785 |
25
JsonService.cpp
Normal file
25
JsonService.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "JsonService.h"
|
||||||
|
#include "thirdparty/json.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
Weather JsonService::getWeather(std::string s) {
|
||||||
|
std::ifstream fin(s);
|
||||||
|
if (!fin)
|
||||||
|
throw std::runtime_error("error"); // Linux support
|
||||||
|
|
||||||
|
json j;
|
||||||
|
j = json::parse(fin);
|
||||||
|
|
||||||
|
std::string city = j["name"]; // Киров
|
||||||
|
double lon = j["coord"]["lon"]; // 49.6601
|
||||||
|
double lat = j["coord"]["lat"]; // 58.5966
|
||||||
|
double temperature = j["main"]["temp"]; // 5.69
|
||||||
|
std::string weather = j["weather"][0]["description"]; // дождь
|
||||||
|
double windSpeed = j["wind"]["speed"]; // 4.27
|
||||||
|
int clouds = j["clouds"]["all"]; // 100
|
||||||
|
|
||||||
|
return Weather(city, lon, lat, temperature, weather, windSpeed, clouds);
|
||||||
|
}
|
||||||
8
JsonService.h
Normal file
8
JsonService.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Service.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class JsonService : public Service {
|
||||||
|
public:
|
||||||
|
virtual Weather getWeather(std::string s) override;
|
||||||
|
virtual ~JsonService() {}
|
||||||
|
};
|
||||||
20
main.cpp
Normal file
20
main.cpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
#include "JsonService.h"
|
||||||
|
#include "Weather.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
JsonService js;
|
||||||
|
Weather w = js.getWeather("endpoints/weather.json");
|
||||||
|
|
||||||
|
std::cout << "Weather object created" << std::endl;
|
||||||
|
w.printWeather();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25526
thirdparty/json.hpp
vendored
Normal file
25526
thirdparty/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user