Reputation: 13156
I'm trying to create a generic template which will render out JSON objects. The problem is, with all the samples I have seen, they are based on knowing the names of the key...
I started off trying something like this:
<table>
{{each}}
<tr>
<td>$($value[0]}</td><td>$($value[1]}</td>
</tr>
{{/each}}
</table>
Now I this isn't generic yet but I was trying this as a start, but it doesn't work...
Upvotes: 2
Views: 1480
Reputation: 922
This was not apparent to me either.
<table>
<tr>
{{each $data}}
<td>${ $index }</td><td>${ $value }</td>
{{/each}}
</tr>
</table>
But when you see it you have a D'OH moment.
Upvotes: 1
Reputation: 13483
Here's some code that will get you the keys and values of your JSON object. I'm basing this off of jQuery JSON Associative Array.
for(var key in obj) {
if (obj.hasOwnProperty(key)){
document.write("<tr><td>" + key + "</td><td>" + obj[key] + "</td></tr>");
}
}
It's standard javascript. There's an shorter way to do this in jQuery (mentioned in the previous link).
Upvotes: 0