使用 boost 属性树遍历 json 属性键和值

Posted

技术标签:

【中文标题】使用 boost 属性树遍历 json 属性键和值【英文标题】:iterating through json property keys and values using boost property tree 【发布时间】:2014-06-30 09:00:43 【问题描述】:

我正在尝试使用boost::property_tree 读取json 文件。我已经能够读取 json 文件并获取属性值。现在,我正在尝试遍历属性树中的所有属性并打印出所有键值对。我不知道该怎么做。我曾尝试使用BOOST_FOREACH,但最终出现编译错误。如何正确遍历属性树变量中的属性并获取它们各自的键和值?

#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <exception>
#include <string>
using namespace std;

    int main()
        boost::property_tree::ptree pt;
        boost::property_tree::read_json("./data.json", pt);
            BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt)
        
            std::cout << v.first << ":" << v.second << std::endl;
        
        std::cout<<pt.get<string>("name")<<std::endl;
        std::cout <<"Done"<<std::endl;
        return 1;
    

我在std::cout &lt;&lt; v.first &lt;&lt; ":" &lt;&lt; v.second &lt;&lt; std::endl; 行收到编译器错误。我正在使用g++ -o test test.c 进行编译。编译错误是:

test.c: In function 'int main()':
test.c:20:42: error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::cout), (* & v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::first))), ((const char*)":")) << v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second'
test.c:20:42: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:106:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:106:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:115:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:115:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) aka std::basic_ios<char>& (*)(std::basic_ios<char>&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:125:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:125:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::ios_base& (*)(std::ios_base&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:164:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:164:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:168:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:168:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:172:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:172:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'bool'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:93:5: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'short int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:179:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:179:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'short unsigned int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:107:5: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:190:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:190:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:199:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:199:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long long int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:203:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:203:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long long unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:218:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:218:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'double'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:222:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:222:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'float'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:230:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:230:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long double'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:243:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:243:7: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'const void*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:121:5: note:   no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__streambuf_type* aka std::basic_streambuf<char>*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/string:54:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/locale_classes.h:42,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:43,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ios:43,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/basic_string.h:2750:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:469:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:469:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   deduced conflicting types for parameter '_CharT' ('char' and 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >')
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:474:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:474:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:480:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:480:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:486:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:486:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'signed char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:491:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:491:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'unsigned char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:511:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:511:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   mismatched types 'const _CharT*' and 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:323:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:323:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:528:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:528:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:541:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:541:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const signed char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
                 from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:546:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:546:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const unsigned char*'
In file included from /usr/include/boost/spirit/home/classic/utility/impl/chset/basic_chset.hpp:13:0,
                 from /usr/include/boost/spirit/home/classic/utility/chset.hpp:16,
                 from /usr/include/boost/spirit/home/classic/utility.hpp:26,
                 from /usr/include/boost/spirit/home/classic.hpp:29,
                 from /usr/include/boost/spirit/include/classic.hpp:11,
                 from /usr/include/boost/property_tree/detail/json_parser_read.hpp:18,
                 from /usr/include/boost/property_tree/json_parser.hpp:14,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bitset:1523:5: note: template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::bitset<_Nb>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bitset:1523:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'const std::bitset<_Nb>'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:78:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Resetiosflags)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:78:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Resetiosflags'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:108:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setiosflags)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:108:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setiosflags'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:142:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setbase)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:142:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setbase'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:177:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setfill<_CharT>)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:177:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'std::_Setfill<_CharT>'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:207:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setprecision)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:207:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setprecision'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
                 from /usr/include/boost/property_tree/json_parser.hpp:15,
                 from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:237:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setw)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:237:5: note:   template argument deduction/substitution failed:
test.c:20:42: note:   cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setw'

【问题讨论】:

您确定文件名为test.c?因为那是一个 C 源文件,应该编译为 C 程序。如果将源文件重命名为 e.g. 会发生什么? test.cpp? 我实际上将它命名为 test.c 而不是 test.cpp。我更改了扩展名,但仍然出现编译错误。将 v.second 更改为 v.second.data() 解决了我的问题。 【参考方案1】:

我将文件命名为 test.c 而不是 test.cpp,所以这可能是个问题。但改变这一点并不能解决问题。我将v.second 更改为v.second.data(),效果很好。

【讨论】:

以上是关于使用 boost 属性树遍历 json 属性键和值的主要内容,如果未能解决你的问题,请参考以下文章

BGL(boost graph Library)(8-)

如何在 AngularJS 中使用 ng-repeat 迭代键和值?

从 json 文件构造 boost 属性树的性能很差?

Android JSONObject - 我如何遍历一个平面 JSON 对象来获取每个键和值

通过使用 boost 属性树解析 JSON 文件来访问布尔值

如果将来添加任何键,json如何解析动态键和值