繁体   English   中英

使用Jekyll / Liquid解析和过滤JSON

[英]Parsing and filtering JSON using Jekyll/Liquid

我尝试在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}} {{stuff | size}}是1,所以我可以期待,它是一个对象,而不是内部的一系列字符。 但是,如果我想继续使用这个结构,我没有输出, stuff.textstuff["text"]等等不起作用。

有人暗示我吗?

如果你使用{{ stuff | inspect }} {{ stuff | inspect }}你可以看到它是一个数组。

您可以使用{% assign stuff = site.data.navigation.main | where:"text","b" | first %}来捕获内部对象 {% assign stuff = site.data.navigation.main | where:"text","b" | first %} {% assign stuff = site.data.navigation.main | where:"text","b" | first %}

暂无
暂无

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

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