According to that question on Soen, how to make an HTTP request depends on the operating system and/or the use of external libraries. According to the accepted answer, the recommended form is through the library libcurl, using one of his bindings for C++. That answer has an example of code using the second link:
// Edit : rewritten for cURLpp 0.7.3
// Note : namespace changed, was cURLpp in 0.7.2 ...
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
// RAII cleanup
curlpp::Cleanup myCleanup;
// standard request object.
curlpp::Easy myRequest;
// Set the URL.
myRequest.setOpt(new curlpp::options::Url(std::string("http://www.wikipedia.com")));
// Send request and get a result.
// By default the result goes to standard output.
// Here I use a shortcut to get it in a string stream ...
std::ostringstream os;
os << myRequest.perform();
string asAskedInQuestion = os.str();
Although it is initially possible to use libcurl in Windows as well, there is an alternative solution that uses only the native libraries of this, in that other answer.
Once the HTTP request has been made and the response has been obtained, simply interpret the JSON. The page json.org lists several library options to do the Parsing. It’s hard to suggest one, because there are so many, and even in Soen* there is a good answer with examples, but I found the Rapidjson at first glance quite simple to use:
// rapidjson/example/simpledom/simpledom.cpp`
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {
// 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}
* Note: broken link (the question on Soen, "What’s the best C++ JSON parser?", has been removed and can only be accessed by anyone who has 10,000 reputation points or more on Soen)
When trying to answer the question, I realized that "making an HTTP request" and "interpreting a JSON" are two sufficiently distinct steps that each deserved a separate question. Anyway, I left my answer.
– mgibsonbr