Handling somewhat complex JSON data with jQuery Templates
I'm not a JS expert but I've been given a JSON model to display using jQuery, and have my hopes up for jQuery Templates. However all the documentation examples seem to be for simple 2-level arrays, and I can't translate these examples to the JSON I have. I'm not clear on whether I should be working with jQuery.tmpl(), or {{each}} or {{tmpl}} or {{wrap}} to iterate through this data.
I need to acquire the JSON below and display each "host" name followed by its "states" details (CPU, Disk, Mem.) Any hints on开发者_开发技巧 how to proceed are much appreciated.
{"health":64,
"hosts":[
{
"name":"Windows2000P",
"states":[
{"name":"CPU","link":"(html link)","bgcolor":"#CC99CC"},
{"name":"Disk","link":"(html link)","bgcolor":"#CC99CC"},
{"name":"Mem","link":"(html link)","bgcolor":"#CC99CC"}
]
},
{
"name":"Windows2003",
"states":[
{"name":"CPU","link":"(html link)","bgcolor":"#98FB98"},
{"name":"Disk","link":"(html link)","bgcolor":"#98FB98"},
{"name":"Mem","link":"(html link)","bgcolor":"#F08080"}
]
},
{
"name":"Windows7",
"states":[
{"name":"CPU","link":"(html link)","bgcolor":"#98FB98"},
{"name":"Disk","link":"(html link)","bgcolor":"#98FB98"},
{"name":"Mem","link":"(html link)","bgcolor":"#F08080"}
]
}
]
}
Here's an example of the kind of template you might use:
<script type="text/x-jquery-tmpl">
<h2>${health}</h2>
{{each hosts}}
<h3>${name}</h3>
<ul>
{{each states}}
<li style="background-color: ${bgcolor};"><a href="${link}">${name}</li>
{{/each}}
</ul>
{{/each}}
</script>
精彩评论