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
13
  • search for json deserializer in c++ Commented Apr 10, 2020 at 6:12
  • CURLcode is a result code for the function call, like success or various failures. 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. Commented Apr 10, 2020 at 6:12
  • this is string returned but its JSON response returned by API, you have to use the JSON library to parse it. Commented Apr 10, 2020 at 6:16
  • This may help you on the road to getting the actual data: stackoverflow.com/questions/9786150/… and this may help you with the json parsing: stackoverflow.com/questions/47283908/… Commented Apr 10, 2020 at 6:17
  • example-code.com/cpp/http_post_json_parse_response.asp Commented Apr 10, 2020 at 6:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.