简体   繁体   English

使用Jekyll / Liquid解析和过滤JSON

[英]Parsing and filtering JSON using Jekyll/Liquid

I try to select and use the sub-tree of my navigation, specified in JSON, in Jekyll. 我尝试在Jekyll中选择并使用JSON中指定的导航子树。

This is the file _data/navigation.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" }
    ] 
}

Now I want to get the subtree of 'b': 现在我想得到'b'的子树:

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

If I print this via {{ stuff }} , jekyll/liquids serves me an one line string representation starting {"text": "b", "children":... , as one could expect. 如果我通过{{ stuff }}打印这个,jekyll / liquids为我提供了一个单行字符串表示,从{"text": "b", "children":... ,正如人们所期望的那样。 {{stuff | size}} {{stuff | size}} is 1, so I can expect, it is one object, instead a sequence of characters internally. {{stuff | size}}是1,所以我可以期待,它是一个对象,而不是内部的一系列字符。 But if I want to go on working with this structure, I get no output, stuff.text , stuff["text"] and so on does not work. 但是,如果我想继续使用这个结构,我没有输出, stuff.textstuff["text"]等等不起作用。

Has someone a hint for me? 有人暗示我吗?

If you use {{ stuff | inspect }} 如果你使用{{ stuff | inspect }} {{ stuff | inspect }} you can see that it is an array. {{ stuff | inspect }}你可以看到它是一个数组。

You can catch the object inside with {% 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 %} {% 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