我的JSON很好。我从json2html网站上看到了一个非常简单的例子......没有任何显示。
<script type="text/javascript">
$.getJSON( "news/list/category/news/format/json", function(data) {
var transforms = {
stories: [
{'<>':'ul','class':'stories','html':function() {
return($.json2html(this.groups,transforms.group));
}}
],
group: [
{"<>": "li", "id":"id", "html":[
{"<>": "span", "html": "${AUTHOR} ${HEADLINE}"}
]}
]
};
$('#stories').json2html(data, transforms.stories);
})
</script>
答案 0 :(得分:1)
首先,如果你可以在你的问题中粘贴实际的JSON,以帮助解决问题。
这可能是你出错的地方
stories: [
{'<>':'ul','class':'stories','html':function() {
return($.json2html(this.groups,transforms.group));
}}
],
让我试试这是如何运作的
第一
$('#stories').json2html(data, transforms.stories);
将数据对象转换为故事
stories: [
{'<>':'ul','class':'stories','html':function() {
return($.json2html(this.groups,transforms.group));
}} ],
Stories转换&#34;组下的对象&#34;财产分组
group: [
{"<>": "li", "id":"id", "html":[
{"<>": "span", "html": "${AUTHOR} ${HEADLINE}"}
]}
]
Group将组对象转换为带有span的列表项。
所以你的json对象更像是
[{"groups":[{"AUTHOR":"Someone","HEADLINE":"Someheadline"},..]}]
虽然因为组只是用作例子,我怀疑你的json对象看起来更像这样(最好在你的问题中粘贴那个实际的JSON)
[{"AUTHOR":"Someone","HEADLINE":"Someheadline"},..]
在这种情况下,只需使用以下代码snipet即可。请记住,转换只是从$ .getJSON
中为JSON数组中的每个对象所需的html(模板)的表示。var data = [{"AUTHOR":"Someone","HEADLINE":"Someheadline"}];
var transforms = {
story: [
{"<>": "li", "id":"id", "html":[
{"<>": "span", "html": "${AUTHOR} ${HEADLINE}"}
]}
]};
$("#stories").json2html(data,transforms.story);
//OR using getJSON
$.getJSON( "news/list/category/news/format/json", function(data) {
$('#stories').json2html(data, transforms.story);
});