'Trying to parse json values in c++ but getting exceptions with nlohmann

I am trying to take a curl request, store it as a string and then parse to JSON so that I can work with the values extracted from the json object.

An example of what I'm trying to do is as follows:

The postman output of the request I give looks like this:

<p>Results from your query:</p><br>{"ID":"26","datetime":"2022-03-13 03:21:07","temperature":"25.3","humidity":"80.9","pressure":"1020.2"}<br>{"ID":"27","datetime":"2022-03-13 05:12:47","temperature":"24.8","humidity":"82.1","pressure":"1020.5"}<br>{"ID":"28","datetime":"2022-03-13 05:29:05","temperature":"24.9","humidity":"83.6","pressure":"1020.5"}<br>{"ID":"29","datetime":"2022-03-13 05:29:07","temperature":"24.9","humidity":"83.8","pressure":"1020.5"}

I want to use C++ to put just the Temperature values in an array. I've used curl to get the response in JSON and I have used nlohmann to try and extract the values I need but nothings working, the code of what I have done is below. Any help would be greatly appreciated as I need this to progress in my assignment.

#include <iostream>
#include <string>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <sstream>
#include <vector>
#include <sstream>
using namespace std;

using json = nlohmann::json;

static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

void parser(std::string s, std::string s1) {
    auto j = json::parse(s);
    std::cout << j.at(s1) << '\n';
}

int main(void)
{

    std::string s;
    CURL* curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, "www.ecobliss.co.za / run_student_query.php ? query = Select % 20 * %20FROM % 20data % 20WHERE % 20ID % 20 % 3C % 2030");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist* headers = NULL;
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        std::cout << s << std::endl;

        //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        res = curl_easy_perform(curl);
        // curl_easy_cleanup(curl);
    }

    std::string target = "<p>Results from your query:</p><br>";
    int found = -1;
    do {
        found = s.find(target, found + 1);

        if (found != -1) {
            s = s.substr(0, found) + s.substr(found + target.length());
        }

    } while (found != -1);

    // int id = arrArduino.value("ID", 0);

    char chars[] = "<br>";

    for (unsigned int i = 0; i < strlen(chars); ++i)
    {
        // you need include <algorithm> to use general algorithms like std::remove()
        s.erase(std::remove(s.begin(), s.end(), chars[i]), s.end());
    }

    //  std::cout << s;
    // parser(s, "ID");
     // std::cout << id;
   

    return 0;
}

As seen above I removed all the html tags but still i get an error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source