Mustache.js 和 YQL jSON
我不太确定如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该将数据扁平化为更简单的格式,以编写有意义的模板,但您可以按原样获得非常接近您想要的内容。
我看到的一个无法克服的问题是“结果”位于“结果”内部,导致标签闭合不匹配。这样做可以解决这个问题:
这里有一个功能示例。
您可以在本文中找到更多胡子技巧。
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:
A functional sample is here.
You can find some more mustache tricks in this article.
这是 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.