我试图做一个最小的例子来读取一个json字符串,该字符串作为带有boost的命令行arg传递.我对C++很陌生并且提升.
我的代码是:
int main (int argc, char ** argv)
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(argv[1], pt);
cout << pt.get<std::string>("foo");
}
Run Code Online (Sandbox Code Playgroud)
我称之为
./myprog "{ \"foo\" : \"bar\" }"
Run Code Online (Sandbox Code Playgroud)
但我收到'无法打开文件错误'.如何获得读取std :: string或char*而不是文件的提升?
谢谢
bst*_*our 34
你可以做的是将字符读入字符串流,然后将其传递给read_json.
#include <sstream>
#include <iostream>
#include <boost/property_tree/json_parser.hpp>
int main (int argc, char ** argv)
{
std::stringstream ss;
ss << argv[1];
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);
std::cout << pt.get<std::string>("foo") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出
bar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12116 次 |
| 最近记录: |