0

I am new to cpp. I am struggling to parse my response from an API.

I have used following code to call REST API.

CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/sample");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "type=video&u_tid=hello&n_frames=100");
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    std::cout << res << std::endl;

}

O/P:

[{"rect":{"height":0.670796573,"k":6,"width":0.767148435,"x":0.874048531,"y":0.884222329}}]

I am successfully getting the required response. I want to parse element as json object.

i.e., python equivalent code is res[0]["rect"]["height"]

I look into the data type of the variable it says,

8CURLcode

I am unable to parse like this also

std::cout << res[0] << '\n';

How to parse my response in c++? any help would be appreciable.

EDIT-1:

As many of you mentioned use json lib, i followed this link.

My updated code:

#include <iostream>
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/writer.h>
#include <jsoncpp/json/value.h>
#include <curl/curl.h>

#include <typeinfo>


using namespace std;


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;
}

int main()
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/sample");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "type=video&u_tid=hello&n_frames=100");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;
    }


    Json::Value root;   
    Json::Reader reader;
    bool parsingSuccessful = reader.parse( readBuffer.c_str(), root );     //parse process
    cout<<parsingSuccessful;
    if ( !parsingSuccessful )
    {
        std::cout  << "Failed to parse"<< reader.getFormattedErrorMessages();

    }
    else
    {
        std::cout << root.get("mykey", "A Default Value if not exists" ).asString() << std::endl;
    }

    return 0;
}

Error:

/tmp/ccXxEiWg.o: In function `main':
json.cpp:(.text+0x155): undefined reference to `Json::Value::Value(Json::ValueType)'
json.cpp:(.text+0x164): undefined reference to `Json::Reader::Reader()'
json.cpp:(.text+0x1c0): undefined reference to `Json::Reader::parse(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Json::Value&, bool)'
json.cpp:(.text+0x21e): undefined reference to `Json::Reader::getFormattedErrorMessages[abi:cxx11]() const'
json.cpp:(.text+0x26a): undefined reference to `Json::Value::Value(char const*)'
json.cpp:(.text+0x28f): undefined reference to `Json::Value::get(char const*, Json::Value const&) const'
json.cpp:(.text+0x2a8): undefined reference to `Json::Value::asString[abi:cxx11]() const'
json.cpp:(.text+0x2e7): undefined reference to `Json::Value::~Value()'
json.cpp:(.text+0x2f6): undefined reference to `Json::Value::~Value()'
json.cpp:(.text+0x319): undefined reference to `Json::Value::~Value()'
json.cpp:(.text+0x3a4): undefined reference to `Json::Value::~Value()'
json.cpp:(.text+0x3b8): undefined reference to `Json::Value::~Value()'
/tmp/ccXxEiWg.o:json.cpp:(.text+0x3e0): more undefined references to `Json::Value::~Value()' follow
collect2: error: ld returned 1 exit status
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
  • search for json deserializer in c++ – Ashish Negi Apr 10 '20 at 06:12
  • `CURLcode` is a result code for the function call, like success or various failures. https://curl.haxx.se/libcurl/c/libcurl-errors.html If you want the data you need to register a function to receive and store that data for later use. Of course, once you have the data you'll need to parse it, which will likely lead to a search for a JSON library to do that. In short, it's not gonna be quite that simple. – Retired Ninja Apr 10 '20 at 06:12
  • this is string returned but its JSON response returned by API, you have to use the JSON library to parse it. – Build Succeeded Apr 10 '20 at 06:16
  • This may help you on the road to getting the actual data: https://stackoverflow.com/questions/9786150/save-curl-content-result-into-a-string-in-c and this may help you with the json parsing: https://stackoverflow.com/questions/47283908/parsing-json-string-with-jsoncpp – Retired Ninja Apr 10 '20 at 06:17
  • https://www.example-code.com/cpp/http_post_json_parse_response.asp – Build Succeeded Apr 10 '20 at 06:22
  • Thanks for the response. I am getting issue with json parsing. – Mohamed Thasin ah Apr 10 '20 at 06:28
  • Paste your build command. – theWiseBro Apr 10 '20 at 06:40
  • @theWiseBro - `g++ json.cpp -lcurl`, here my filename is `json.cpp` – Mohamed Thasin ah Apr 10 '20 at 06:41
  • `jsoncpp` is a third-party library. You have to build its source as well and link it again your source file. Read the instructions at https://github.com/open-source-parsers/jsoncpp on how to build it. – theWiseBro Apr 10 '20 at 06:46
  • Continuing on above, you can try nlohmann json instead because of its single include header. So you can just include the header and that's it. – theWiseBro Apr 10 '20 at 06:47
  • @theWiseBro - I cloned the nlohman project. still it says, json.cpp:3:29: fatal error: nlohmann/json.hpp: No such file or directory. should I add anything else? – Mohamed Thasin ah Apr 10 '20 at 06:57
  • Actually you need just this file `https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp`. Even if you've cloned, include it like `#include "path-to-this-file"`, for eg. `#include "json/single_include/nlohmann/json.hpp"` – theWiseBro Apr 10 '20 at 07:04
  • @theWiseBro - you are right. it's working :) – Mohamed Thasin ah Apr 10 '20 at 07:36

0 Answers0