0

我正在拉入一个 json 对象,这是$.parseJSON输出的结果。我知道它需要一个处理程序来帮助它,但不确定什么属于帮助程序。阅读其他用户的问题,由于拥有一个恒定的密钥,他们似乎能够跳过下一圈,不幸的是,在我的情况下,它总是不同的。


json输出

[
{
    "High blood pressure?": [
        "no",
        "string"
    ]
},
{
    "Cancer?": [
        "no",
        "string"
    ]
},
{
    "Asthma or a breathing disorder?": [
        "no",
        "string"
    ]
}
]

环形


{{#each screen_data}}
<tr>
    <td class="bold">{{this}}</td>
</tr>
{{/each}}

结果是 [Object object][Object object][Object object]......

4

2 回答 2

0

这是因为你有一个对象数组,这就是你告诉你的模板写出的......对象。似乎您想在对象的根上写出唯一的属性,这是一个问题。

尝试这个:

{{#each screen_data}}
<tr>
    <td class="bold">{{this[0]}}</td>
</tr>
{{/each}}

我必须说,这是一个奇怪的 JSON 结构。通常,将对象属性名称用作此类数据的载体被认为是不良形式。

编辑:我建议将该结构更改为更能代表您的数据的结构......就像这样:

[{
    question: "High blood pressure?",
    answers: [
        "no",
        "string"
    ]
},
{
    questions: "Cancer?",
    answers: [
        "no",
        "string"
    ]
},
{
    question: "Asthma or a breathing disorder?",
    answers: [
        "no",
        "string"
    ]
}]

这意味着您的模板将如下所示:

{{#each screen_data}}
<tr>
    <td class="bold">{{this.question}}</td>
</tr>
{{/each}}
于 2012-10-24T02:58:45.120 回答
0

你可以在下面试试这个小提琴。它将为您提供 JSON 数据中的所有密钥

http://jsfiddle.net/tariqulazam/SjugS/

var data= [
{
    "High blood pressure?": [
        "no",
        "string"
    ]
},
{
    "Cancer?": [
        "no",
        "string"
    ]
},
{
    "Asthma or a breathing disorder?": [
        "no",
        "string"
    ]
}
];

for (var key in data) {
       for (var item in data[key]) {    
           alert(item);
       }
}
于 2012-10-24T02:49:40.597 回答