1

curl_jsoncpp_example.cpp : https://gist.github.com/connormanning/41efa6075515019e499c

#include <json/json.h> 

g++ main.cpp -ljsoncpp -lcurl -o example.out

so error : main.cpp:8:23: fatal error: json/json.h: No such file or directory

server did not support this, so i edited this section

#include <jsoncpp/json/json.h>

And now I'm faced with this error :

In file included from /usr/include/c++/5/cstdint:35:0,
                 from main.cpp:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
main.cpp: In function ‘int main()’:
main.cpp:44:5: error: ‘unique_ptr’ is not a member of ‘std’
     std::unique_ptr<std::string> httpData(new std::string());
     ^
main.cpp:44:32: error: expected primary-expression before ‘>’ token
     std::unique_ptr<std::string> httpData(new std::string());
                                ^
main.cpp:44:60: error: ‘httpData’ was not declared in this scope
     std::unique_ptr<std::string> httpData(new std::string());

I wanted to use this https://linux.tips/programming/how-to-install-and-use-json-cpp-library-on-ubuntu-linux-os But this is only for files inside the server

I want to get the information from a link json that is and print

exampel json link : http://date.jsontest.com

ptint json data ["time"] C++ or C ?

If you are familiar with PHP, this code is simply written :

<?php
$time = json_decode(file_get_contents('http://date.jsontest.com'),true);
echo $time['time']; 
?>

But how to write in C++ or C ?

Please help me

5
  • 1
    Did you try to read the error message? Commented Apr 25, 2018 at 3:09
  • Yes . I've written errors on the top question - You have a good source to get information from a link json api ? Commented Apr 25, 2018 at 4:15
  • 1
    The answer is in the error message. Look at this carefully. Commented Apr 25, 2018 at 4:36
  • cplusplus.com/forum/general/115510 : But not solved error Commented Apr 25, 2018 at 7:30
  • 2
    1. Learn how to read compiler errors 2. Learn how to use jsoncpp alone 3. Learn how to use libcurl alone 4. Combine 1+2+3 4. PROFIT! Commented Apr 25, 2018 at 9:28

2 Answers 2

1

example.cpp :

// telegram : @ELsAnEHSAN - c++ 
// g++ example.cpp -o example -lcurl -ljsoncpp

#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>

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(void)
{
  CURL *curl;
  CURLcode res;
  std::string readBuffer;
  curl = curl_easy_init();
      if(curl) {
            curl_easy_setopt(curl, CURLOPT_URL, "http://date.jsontest.com/");
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
      }
    Json::Reader reader;
    Json::Value obj;
    reader.parse(readBuffer, obj); 
    std::cout << obj["time"].asString() << std::endl;
  return 0;
}

If needed :

:~$ sudo apt-get install libcurl-dev
:~$ sudo apt-get install libcurl4-openssl-dev
:~$ sudo apt-get install libcurl4-gnutls-dev
:~$ sudo apt-get install libjson-c-dev
:~$ sudo apt-get install libjsoncpp-dev

compiling : g++ example.cpp -o example -lcurl -ljsoncpp

run : ./example

Sign up to request clarification or add additional context in comments.

Comments

0

As S.M mentioned,the error message gives the solution.

Try compiling your program with the: -std=c++11 flag and see if it works.

If it doesn't, please post more code and all the error/warnings you get

Comments

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.