Mustache.js 和 YQL jSON

发布于 2024-10-21 05:22:50 字数 840 浏览 1 评论 0原文

我不太确定如何使用 Mustache.js 从这个嵌套对象中获取数据。我正在使用 YQL 多重查询,它带回嵌套的数据对象 https://gist.github.com/866247。当我尝试访问嵌套对象时,我的问题就出现了。

数据像这样返回,其中 0 和 1 是其中包含对象的数组的一部分。这是树的图片 http://cl.ly/1e1b3O3U233e2I0d3g2f

   query
       results
          results
               0
               1

我已经尝试过下面的模板,但没有得到任何回报。我相信问题在于第二个“结果键”随数组返回,而小胡子不知道该怎么做。

   "{{#query}}
                {{#results}}
                    {{#results}}
                        {{#photo}}
                            {{farm}}
                        {{#photo}}
                    {{/results}}
                {{/results}}
            {{/query}}"

如果这对任何人都有意义,那么如果没有密钥,我如何访问数组中的嵌套对象?

I am not really sure how to get data out of this nested object with Mustache.js. I am using the YQL multi query that brings back my data objects nested https://gist.github.com/866247. My problem comes when I am trying to access the nested objects

The data comes back like this where 0 and 1 are part of an array with objects in them. Here is a picture of the tree http://cl.ly/1e1b3O3U233e2I0d3g2f.

   query
       results
          results
               0
               1

I have tried the template below and I don't get anything back. I believe the problem is that that second "results key" comes back with the array and mustache doesn't know what to do.

   "{{#query}}
                {{#results}}
                    {{#results}}
                        {{#photo}}
                            {{farm}}
                        {{#photo}}
                    {{/results}}
                {{/results}}
            {{/query}}"

If this makes sense to anyone, how do I access the nested objects in the array if there is no key to them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2024-10-28 05:22:51

我认为您应该将数据扁平化为更简单的格式,以编写有意义的模板,但您可以按原样获得非常接近您想要的内容。

我看到的一个无法克服的问题是“结果”位于“结果”内部,导致标签闭合不匹配。这样做可以解决这个问题:

 data.query.flattened = data.query.results.results;

这里有一个功能示例。

您可以在本文中找到更多胡子技巧。

I think you should flatten your data into a simpler format to write a template that makes sense, but you can get pretty close to what you want with it as-is.

The one insurmountable issue I saw was that 'results' is inside 'results' resulting in a tag closure mismatch. Doing something like this solves that problem:

 data.query.flattened = data.query.results.results;

A functional sample is here.

You can find some more mustache tricks in this article.

趴在窗边数星星i 2024-10-28 05:22:51

这是 Handlebars 试图解决的 Mustache 问题之一 - 遍历模板中的对象:

http:// /handlebars.strobeapp.com/#paths

您可以直接访问它,就像它是目录结构一样。我知道这本身并不是您问题的答案,但除了扁平化数据之外,它是一种替代解决方案。

This is one of the types of issues with Moustache that Handlebars attempts to address - traversing through objects in the template:

http://handlebars.strobeapp.com/#paths

Where you access it directly as if it's a directory structure. I know this isn't an answer to your question, per-se, but aside from flattening the data it's an alternative solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文