【问题标题】:Jekyll and Json files for data - How to retrieve the right data in a general way?用于数据的 Jekyll 和 Json 文件 - 如何以一般方式检索正确的数据?
【发布时间】:2015-09-11 09:15:08
【问题描述】:

假设有一个这种格式的json:

{
    "components":[
        {
            "id": "c3-1",
            "type":"charts",
            "component":"ChartC3Line",
            "description":"Chart with round",
            "picture": "/assets/img/components/chartc3-round.png",
            "project": "yyy",
            "date": "06-07-2015",
            "dateupdate": "08-07-2015",
            "status":" production"
        },

        {
            "id": "c3-2",
            "type":"charts",
            "component":"ChartC3Date",
            "description":"Chart with Date",
            "picture": "/assets/img/components/chartc3-withdate.png
id: c3-2",
            "date": "06-07-2015",
            "project": "xxx",
            "dateupdate": "08-07-2015",
            "status":" production"
        }]

}

我希望能够检索关于 ID 为 c3-2 的第二个对象的信息。

前提:

  • 我不想通过索引来获取它;

我怎样才能通过价值获得它?以 id 为例。 当然我必须改变我的 json 文件的结构。

你将如何改变它? 现在我可以通过索引获得正确的值:

{% assign comp = site.data.components.components[1] %}
                    {{ comp.component }}

【问题讨论】:

    标签: json jekyll


    【解决方案1】:

    您可以像这样构建您的 _data/components.json:

    {
        "c3-1" : {
                "type":"charts",
                "component":"ChartC3Line",
                "description":"Chart with round"
        },
        "c3-2" : {
                "type":"charts",
                "component":"ChartC3Date",
                "description":"Chart with Date"
        }
    }
    

    您现在可以通过他的 id 获取一个元素:{{ site.data.components["c3-1"] }}

    【讨论】:

    • 我正在尝试使用 {{ site.data.components["c3-1"].type }} 但什么也没有出现!
    • 对我来说 {{ site.data.components["c3-1"].type }} 有效。你有要测试的 githuv 存储库吗?
    猜你喜欢
    • 2018-04-02
    • 2020-03-31
    • 2021-10-06
    • 1970-01-01
    • 2022-01-21
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多