curl_jsoncpp_example.cpp:https://gist.github.com/connormanning/41efa6075515019e499c
#include <json/json.h>
g ++ main.cpp -ljsoncpp -lcurl -o example.out
错误: main.cpp:8:23:致命错误:json / json.h:没有这样的文件或目录
服务器不支持此功能,所以我编辑了这一部分
#include <jsoncpp/json/json.h>
现在我遇到了这个错误:
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());
我想用这个https://linux.tips/programming/how-to-install-and-use-json-cpp-library-on-ubuntu-linux-os 但这仅适用于服务器内的文件
我想从链接json获取信息,即打印
exampel json link:http://date.jsontest.com
ptint json data [“time”] C ++或C?
如果您熟悉PHP,则只需编写此代码:
<?php
$time = json_decode(file_get_contents('http://date.jsontest.com'),true);
echo $time['time'];
?>
但是如何用C ++或C编写?
请帮帮我
答案 0 :(得分:0)
正如S.M所提到的,错误信息提供了解决方案。
尝试使用以下代码编译程序:
-std=c++11
标记并查看它是否有效。
如果没有,请发布更多代码和所有错误/警告
答案 1 :(得分:0)
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;
}
如果需要:
:~$ sudo apt-get install libcurl-dev
:~$ sudo apt-get install libcurl4-openssl-dev
:~$ sudo apt-get install libcurl4-gnutls-dev
编译: g ++ example.cpp -o example -lcurl -ljsoncpp
运行: ./示例