Getting values from a json file using boost/property_tree, with multiple elements/arrays/sub-arrays C++

后端 未结 2 608
一个人的身影
一个人的身影 2020-12-20 06:19

The facts:

  • I am using VS2013 and developing in C++.
  • I am using boost API to get the values from a standard/legit json file.
  • I can\'t extrac
相关标签:
2条回答
  • 2020-12-20 07:00

    In case you like to have an alternative method, you could use the ad-hoc parser I posted in an earlier answer (Reading JSON file with C++ and BOOST):

    Live On Coliru

    #include <boost/fusion/adapted/std_pair.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <map>
    
    namespace qi = boost::spirit::qi;
    
    std::string const sample = R"(
    {
       "background": {
          "scripts": [ "name1.js", "name2.js", "name3.js" ]
       },
          "default_popup": "popup.html",
          "default_title": "__MSG_name__",
       "content_scripts": [ {
          "all_frames": true,
          "js": [ "name4.js", "name5.js", "name6.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_start"
       }, {
          "all_frames": true,
          "js": [ "include.postload.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_end"
       } ]
    })";
    
    namespace qd_json { // quick and dirty JSON handling
        struct null {};
        using text   = std::string;
        using value  = boost::make_recursive_variant<
                null,
                text,                                      // "string" (roughly!)
                double,                                    // number
                std::map<text, boost::recursive_variant_>, // object
                std::vector<boost::recursive_variant_>,    // array
                bool
            >::type;
        using member = std::pair<text, value>;
        using object = std::map<text, value>;
        using array  = std::vector<value>;
    
        template <typename It, typename Skipper = qi::space_type>
        struct grammar : qi::grammar<It, value(), Skipper>
        {
            grammar() : grammar::base_type(value_) {
                using namespace qi;
    
                text_   = '"' >> raw [*('\\' >> char_ | ~char_('"'))] >> '"';
                null_   = "null" >> attr(null{});
                bool_   = "true" >> attr(true) | "false" >> attr(false);
                value_  = null_ | bool_ | text_ | double_ | object_ | array_;
                member_ = text_ >> ':' >> value_;
                object_ = '{' >> -(member_ % ',') >> '}';
                array_  = '[' >> -(value_  % ',') >> ']';
    
                ////////////////////////////////////////
                // Bonus: properly decoding the string:
                text_   = lexeme [ '"' >> *ch_ >> '"' ];
    
                ch_ = +(
                        ~char_("\"\\")) [ _val += _1 ] |
                           qi::lit("\x5C") >> (               // \ (reverse solidus)
                           qi::lit("\x22") [ _val += '"'  ] | // "    quotation mark  U+0022
                           qi::lit("\x5C") [ _val += '\\' ] | // \    reverse solidus U+005C
                           qi::lit("\x2F") [ _val += '/'  ] | // /    solidus         U+002F
                           qi::lit("\x62") [ _val += '\b' ] | // b    backspace       U+0008
                           qi::lit("\x66") [ _val += '\f' ] | // f    form feed       U+000C
                           qi::lit("\x6E") [ _val += '\n' ] | // n    line feed       U+000A
                           qi::lit("\x72") [ _val += '\r' ] | // r    carriage return U+000D
                           qi::lit("\x74") [ _val += '\t' ] | // t    tab             U+0009
                           qi::lit("\x75")                    // uXXXX                U+XXXX
                                >> _4HEXDIG [ append_utf8(qi::_val, qi::_1) ]
                        );
    
                BOOST_SPIRIT_DEBUG_NODES((text_)(value_)(member_)(object_)(array_)(null_)(bool_))
            }
        private:
            qi::rule<It, text()>            text_, ch_;
            qi::rule<It, null()>            null_;
            qi::rule<It, bool()>            bool_;
            qi::rule<It, value(),  Skipper> value_;
            qi::rule<It, member(), Skipper> member_;
            qi::rule<It, object(), Skipper> object_;
            qi::rule<It, array(),  Skipper> array_;
    
            struct append_utf8_f {
                template <typename...> struct result { typedef void type; };
                template <typename String, typename Codepoint>
                void operator()(String& to, Codepoint codepoint) const {
                    auto out = std::back_inserter(to);
                    boost::utf8_output_iterator<decltype(out)> convert(out);
                    *convert++ = codepoint;
                }
            };
            boost::phoenix::function<append_utf8_f> append_utf8;
            qi::uint_parser<uint32_t, 16, 4, 4> _4HEXDIG;
        };
    
        template <typename Range, typename It = typename boost::range_iterator<Range const>::type>
        value parse(Range const& input) {
            grammar<It> g;
    
            It first(boost::begin(input)), last(boost::end(input));
            value parsed;
            bool ok = qi::phrase_parse(first, last, g, qi::space, parsed);
    
            if (ok && (first == last))
                return parsed;
    
            throw std::runtime_error("Remaining unparsed: '" + std::string(first, last) + "'");
        }
    
        namespace accessors {
            static double          dbl_(qd_json::value const&v) { return boost::get<double>(v); };
            static int             int_(qd_json::value const&v) { return boost::get<double>(v); };
            static std::string     txt_(qd_json::value const&v) { return boost::get<qd_json::text>(v); };
            static qd_json::array  arr_(qd_json::value const&v) { return boost::get<qd_json::array>(v); };
            static qd_json::object obj_(qd_json::value const&v) { return boost::get<qd_json::object>(v); };
        }
    }
    
    using It = std::string::const_iterator;
    
    int main()
    {
        using namespace qd_json::accessors;
    
        auto root = obj_(qd_json::parse(sample));
    
        for(auto& o : arr_(root["content_scripts"]))
            for(auto& f : arr_(obj_(o)["js"]))
                std::cout << txt_(f) << "\n";
    }
    

    Prints

    name4.js
    name5.js
    name6.js
    include.postload.js
    
    0 讨论(0)
  • 2020-12-20 07:06

    get<std::string>("js") can't work because "js" has an array value.

    for(auto& e : pt.get_child("content_scripts"))
        for(auto& r : e.second.get_child("js"))
            std::cout << r.second.get_value<std::string>() << "\n";
    

    should do

    Live On Coliru

    #include <sstream>
    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/json_parser.hpp>
    #include <iostream>
    
    std::string const sample = R"(
    {
       "background": {
          "scripts": [ "name1.js", "name2.js", "name3.js" ]
       },
          "default_popup": "popup.html",
          "default_title": "__MSG_name__",
       "content_scripts": [ {
          "all_frames": true,
          "js": [ "name4.js", "name5.js", "name6.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_start"
       }, {
          "all_frames": true,
          "js": [ "include.postload.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_end"
       } ]
    })";
    
    using boost::property_tree::ptree;
    namespace j = boost::property_tree::json_parser;
    
    int main() {
        std::istringstream iss(sample);
        ptree pt;
        j::read_json(iss, pt);
    
        for(auto& e : pt.get_child("content_scripts"))
            for(auto& r : e.second.get_child("js"))
                std::cout << r.second.get_value<std::string>() << "\n";
    }
    

    Prints

    name4.js
    name5.js
    name6.js
    include.postload.js
    
    0 讨论(0)
提交回复
热议问题