OOP – Comp 345
Open Weather Map - JSON & C++ Project
Goal – To understand and be able to properly apply the following topics and tools:
1. NuGet Package Manager
2. JSON – Java Script Object Notation
3. Object Oriented encapsulation and information hiding
4. Using Casablanca to make an http call from C++
5. Vector/List template from the C++ STL
In this project you will read the 5 day weather forecast from [Link] in the form of a JSON
string. You will populate a list of DailyWeatherForecast objects, then give the user a menu from which they
can view:
1. the forecast for a user entered zip code
2. the forecast for all 5 days
3. the forecast for a specific day
4. the lowest and highest temperatures from all forecasts
5. at least one other creative option
Tasks:
1. (15 points) Make a C++ type that models the data source(s)
a. Use proper encapsulation
b. Use information hiding
2. (15 points) Make a C++ program that will get the JSON data from an HTTP site and populate STL
list(s) or your type(s)
3. (20 points) 4 points for each menu option listed above
To accomplish this you must first download the Casablanca package using the NuGet package manager
console.
Or on the package manager condole:
PM> Install-Package cpprestsdk
DO NOT INCLUDE THE cpprestsdk FILES IN YOUR SUBMISSION TO EASEL.
NOTE From 2018/08/27: Today when I downloaded this package I had to change a few lines of code in the
ccprestsdk library.
Remove datetime:: from line 491 in asyncrt_utils.h
Remove #ifndef _WIN32 // Required by GCC #endif
o From around typename on line 886 in streams.h
o From around typename on line 895 in streams.h
Signup for an API key at: [Link]
NOTE: This package is large. Downloading this package takes a while (3 min or so). Do not submit this as
part of your solution on Easel.
Helpful Links:
[Link]
[Link]
Open Weather Map – Documentation for current weather API
[Link]
Open Weather
Map API key - [Link]
Example Request -
[Link]
118ec73263ed1ff985&units=imperial
This may help to convert a string in c++ to the string type used in the library.
conversions::to_string_t(string variable);
The code for reading from the json response should look similar to this (but you should be saving the values
in an object and the object in a list).
#include<iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace std;
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams;
using std::string;
void main(){
http_client client(U("[Link]
uri_builder builder(U("/data/2.5/forecast"));
builder.append_query(U("zip"), U("72143"));
builder.append_query(U("appid"), U("5ae2ce314387f8118ec73263ed1ff985"));
builder.append_query(U("units"), U("imperial"));
builder.append_query(U("mode"), U("json"));
http_response response = [Link](methods::GET, builder.to_string()).get();
web::json::value forecastJson = response.extract_json(true).get();
web::json::value forecastListJson = [Link](U("list"));
if (forecastListJson.is_array()){
for (int i = 0; i < [Link](); i++)
{
web::json::value forecastDayJson = forecastListJson[i];
web::json::value mainJson = [Link](U("main"));
web::json::value tempJson = [Link](U("temp"));
cout << tempJson.as_double() << endl;
web::json::value weatherJson = [Link](U("weather"))[0];
web::json::value mainWeatherJson = [Link](U("main"));
cout << conversions::to_utf8string(mainWeatherJson.as_string()) << endl;
}
}
}