代码之家  ›  专栏  ›  技术社区  ›  marrowgari

使用flex/bison解析json数据

  •  0
  • marrowgari  · 技术社区  · 6 年前

    我正在接收以下格式的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 calendar api可以使用的有效查询。

    关于如何做到这一点的任何提示或帮助,以证明最佳实践?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Chris Dodd    6 年前

    flex/bison对于json来说有点过分了,但是这里有一个简单的bison解析器可以帮助您开始:

    %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中是留给读者的练习…