C++ 要从url解析的JsonCpp
curl_jsoncpp_example.cpp:C++ 要从url解析的JsonCpp,c++,json,api,C++,Json,Api,curl_jsoncpp_example.cpp: #包括 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,
#包括
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());
在/usr/include/c++/5/cstdint:35:0中包含的文件中,
来自main.cpp:2:
/Ur/Cube/C++/5/BIT/C++ +0xYAdvult.H:32:2:错误:{错误:该文件需要编译器和库支持ISO C++ 2011标准。必须使用-std=c++11或-std=gnu++11编译器选项启用此支持。
#错误此文件需要编译器和库支持\
^
main.cpp:在函数“int main()”中:
main.cpp:44:5:错误:“unique_ptr”不是“std”的成员
std::unique_ptr httpData(新std::string());
^
main.cpp:44:32:错误:在“>”标记之前应该有主表达式
std::unique_ptr httpData(新std::string());
^
main.cpp:44:60:错误:“httpData”未在此作用域中声明
std::unique_ptr httpData(新std::string());
我想用这个
但这仅适用于服务器内的文件
我想从一个链接json获取信息,该链接是json并打印出来的
exampel json链接:
PtIt JSON数据[时间] C++或C?< /P> 如果您熟悉PHP,以下代码只需编写:
<?php
$time = json_decode(file_get_contents('http://date.jsontest.com'),true);
echo $time['time'];
?>
<> >强>但是如何在C++或C?< /强> 中编写?
请帮助我正如S.M提到的,错误消息给出了解决方案 请尝试使用以下命令编译您的程序:
-std=c++11
标记并查看它是否工作
如果没有,请发布更多代码以及您收到的所有错误/警告
编译:g++example.cpp-o example-lcurl-ljsoncpp
运行:/example
您是否尝试读取错误消息?是。我在第一个问题上写了一些错误-您有一个从链接json api获取信息的好来源吗?答案在错误消息中。仔细看这个但没有解决错误1。了解如何读取编译器错误2。学习如何单独使用
jsoncpp
3。了解如何单独使用libcurl
。合并1+2+3+4。利润!
<?php
$time = json_decode(file_get_contents('http://date.jsontest.com'),true);
echo $time['time'];
?>
// 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
:~$ sudo apt-get install libjson-c-dev
:~$ sudo apt-get install libjsoncpp-dev