使用Flex / Bison解析JSON数据

时间:2018-07-05 20:21:21

标签: bison flex-lexer

我正在接收以下格式的json数据...

{
    "timestamp":55759,
    "sttOutput":
    {
        "time":55759,"words":
        [
            {"startTime":1.81,"duration":0.31,"word":"HEY","score":0.97},
            {"startTime":2.14,"duration":0.31,"word":"WHERE'S","score":0.87},
            {"startTime":2.45,"duration":0.47,"word":"FACEBOOK","score":1.0},
            {"startTime":2.92,"duration":0.57,"word":"TRADING","score":1.0}
        ]
    },
    "kwsOutput":
    {
        "time":55759,"words":
        [
            {"startTime":1.96,"duration":0.19,"word":"FACEBOOK=stock ","score":0.63},
            {"startTime":1.82,"duration":0.55,"word":"WHERE=quest IS FACEBOOK=stock TRADING=stock-act ","score":0.74},
            {"startTime":1.83,"duration":0.55,"word":"WHERE'S=quest FACEBOOK=stock TRADING=stock-act ","score":0.85}
        ]
    }
}

我想将具有flex / bison的数据解析为Google日历API可以使用的有效查询。

是否有任何技巧或帮助来证明最佳实践?

1 个答案:

答案 0 :(得分:1)

Flex / bison对于JSON来说算是过高了,但这是一个简单的野牛解析器,可以帮助您入门:

%token STRING NUMBER TRUE FALSE NULL
%%
object: '{' obj_elems '}' | '{' '}' ;
obj_elems: STRING ':' value | obj_elems ',' STRING ':' value ;
vector: '[' vec_elems ']' | '[' ']' ;
vec_elems: value | vec_elems ',' value ;
value: STRING | NUMBER | TRUE | FALSE | NULL | object | vector ;

解压到您的查询API中是读者的一项练习...