使用 Jekyll/Liquid 解析和过滤 JSON

Parsing and filtering JSON using Jekyll/Liquid

我尝试 select 并在 Jekyll 中使用 JSON 中指定的导航子树。

这是文件 _data/navigation.json:

    {
    "main": [
        {"text": "a", "children" : [
            {"text": "aa" },
            {"text": "ab" }
            ]},
        {"text": "b", "children": [
            {"text": "ba", "children": [
                {"text": "baa" },
                {"text": "bab" },
                {"text": "bac" }
                ] },
            {"text": "bb", "children": [
                {"text": "bba" },
                {"text": "bbb" },
                {"text": "bbc" }
                ]},
            {"text": "bc", "children": [
                {"text": "bca"},
                {"text": "bcb"}
                ]},
            {"text": "bd" },
            {"text": "be" }
            ]},
        {"text": "d" },
        {"text": "e"},
        {"text": "f" },
        {"text": "g" }
    ] 
}

现在我想获取'b'的子树:

{% assign stuff = site.data.navigation.main | where:"text","b"  %}

如果我通过 {{ stuff }} 打印此内容,jekyll/liquids 会像预期的那样为我提供以 {"text": "b", "children":... 开头的单行字符串表示形式。 {{stuff | size}} 是 1,所以我可以预料,它是一个对象,而不是内部的一系列字符。但是如果我想继续使用这个结构,我就得不到任何输出,stuff.textstuff["text"] 等等都不起作用。

有人给我提示吗?

如果用{{ stuff | inspect }}可以看出是数组

你可以用{% assign stuff = site.data.navigation.main | where:"text","b" | first %}捕捉里面的物体。