简体   繁体   English

找到某个单词后如何解析数字

[英]How to parse number after finding some word

I want to parse undermentioned JSON and extract from it productionYear value我想解析下面提到的 JSON 并从中提取 productionYear 值

    auto data = {
        "cars" : [
           {
              "name" : "BMW",
              "engine" : 3.0
           },
           {
              "name" : "Citroen",
              "engine" : 3.6
           },
           {
              "name" : "Ferrari",
              "engine" : 4.2
           }
        ],
        "productionYear" : 1999
    }

and I have the following rule for that:我有以下规则:

    using boost::spirit::ascii::string;
    using boost::spirit::omit;
    using boost::spirit::qi::lit;
    using boost::spirit::qi::char_;

    boost::spirit::qi::rule<std::string::iterator, int()> production_;
    production_ = omit[*(char_ - "productionYear") >>
                     lit('"') >> lit(' ') >> lit(':') >> lit(' ')] >> int_;

    int year;
    auto it = data.begin();

    if (boost::spirit::qi::parse(it, data.end(), production_, year))
    {
        std::cout << "Parse finished with succeded status"
                  << std::distance(data.begin(), it);
    }

The parser fails with last iterator position: 0解析器失败,最后一个迭代器位置:0

Can anybody tell me what am I doing wrong ?谁能告诉我我做错了什么?

DISCLAIMER免责声明

Do not do this.要这样做。 Use a JSON parser.使用 JSON 解析器。 There are plenty.有很多。 Your grammar is brittle in a gazillion ways.你的语法在无数方面都很脆弱。 (You will stop on productionYear inside another value, as partial match in a longer key, as a property of nested/sibling objects, UNICODE escapes, escapes. You will not allow valid JSON optional whitespace, etc.). (您将在另一个值内的productionYear停止,作为较长键中的部分匹配,作为嵌套/同级对象的属性,UNICODE 转义、转义。您将不允许使用有效的 JSON 可选空格等)。

See here for what it takes to parse JSON in Spirit, to a reasonable extent: https://github.com/sehe/spirit-v2-json/blob/master/json.cpp在此处查看在合理范围内解析 Spirit 中的 JSON 所需的内容: https : //github.com/sehe/spirit-v2-json/blob/master/json.cpp

*(char_ - "productionYear") 

Parses any text up to (but not including) productionYear .解析任何文本直到(但不包括) productionYear

This means that the next character never matches '"' (because it's 'p' ).这意味着下一个字符永远不会匹配'"' (因为它是'p' )。

A straight forward fix is一个直接的解决方法是

#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main() {
    std::string const data = R"({
        "cars": [{
            "name": "BMW",
            "engine": 3.0
        },
        {
            "name": "Citroen",
            "engine": 3.6
        },
        {
            "name": "Ferrari",
            "engine": 4.2
        }],
        "productionYear" : 1999
    })";

    boost::spirit::qi::rule<std::string::const_iterator, int()> production_;
    production_ = qi::omit[+(qi::char_ - "productionYear") >> "productionYear\"" >> ' ' >> ':' >> ' '] >> qi::int_;

    int year;
    auto it = data.begin(), last = data.end();

    if (qi::parse(it, last, production_, year)) {
        std::cout << "Parsed: " << year << "\n";
    } else {
        std::cout << "Parsed failed\n";
    }

    if (it != last)
        std::cout << "Remaining input: '" << std::string(it, last) << "'\n";
}

Live On Coliru 住在 Coliru

Output:输出:

Parsed: 1999
Remaining input: '
    }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM