【问题标题】:Parsing and filtering JSON using Jekyll/Liquid使用 Jekyll/Liquid 解析和过滤 JSON
【发布时间】:2017-04-25 00:27:17
【问题描述】:

我尝试在 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"] 等等都不起作用。

有人给我提示吗?

【问题讨论】:

    标签: json jekyll liquid


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2023-01-20
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多