C++ C++;boost属性树将json存储为字符串
您好,我正在尝试存储一个json字符串中的某些值,我在使用CURL时从API中获取该字符串 当前代码:C++ C++;boost属性树将json存储为字符串,c++,json,boost,curl,boost-propertytree,C++,Json,Boost,Curl,Boost Propertytree,您好,我正在尝试存储一个json字符串中的某些值,我在使用CURL时从API中获取该字符串 当前代码: #include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <string> #include <curl/curl.h> // -lcurl #include <iostream> using n
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <string>
#include <curl/curl.h> // -lcurl
#include <iostream>
using namespace std;
using namespace boost;
using boost::property_tree::ptree;
using boost::property_tree::read_json;
void curl_query() {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, query_id.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
cout << readBuffer << endl; // json string here - - work's, just messy
}
ptree pt;
read_json(readBuffer, pt);
// so here I'd like to extract a specific part
string jsonBuffer = pt.get_child<std::string>("current.price");
cout << endl << jsonBuffer << endl;
}
还有更多的文本,我没有看到明显的错误,但是我当前的代码中有一些错误
请注意:将read_json
(从std::string或file读取)替换为write_json
(尝试写入文件)可以很好地编译,但由于无法创建/写入文件,因此会获得核心转储。不过我不想写json,所以我不确定我的代码目前出了什么问题
任何建议、链接、提示/提示-非常感谢。对于名称空间,如何
名称空间pt=boost::property_tree
然后您可以使用pt::ptree
代替。一个错误是您在readBuffer
上调用get\u child()
,而不是pt
。但您可能想说类似于const std::string s=pt.get(“current.price”)代码>。请看。谢谢你们两位的评论,@JoachimPileborg-我让名称空间正常工作了-谢谢。rhashimoto这确实是我的一个错误,但是现在它抛出了一大堆错误(包含在中的文件中)等等。我已经用当前给出的代码/错误更新了帖子,但仍然没有进展:(你需要学习如何从编译器读取错误消息,以获得C++的任何地方,特别是在Boost中。通常,从模板错误中浏览这些内容是很乏味的,但是这是手工操作的必要部分。首先看看编译器在代码中引用了什么地方,因为这几乎就是问题所在。编译器的所有这些行告诉你在哪里,编译器的一行告诉你为什么。阅读关于你使用的类型和函数的文档。这里有一个提示-你调用了错误的成员函数。
‘In file included from /usr/include/boost/property_tree/json_parser.hpp:10:0,’