我有JSON数据,如下所示:
[{“tabname”:“orders”,“datagroups”:[{“dataname”:“ordersToday”,“datavalue”:87},
{“dataname”:“orders30Days”,“datavalue”:305}]},{“tabname”:“second”,“datagroups”:
[{“dataname”:“Test”,“datavalue”:17}]}]
我的转换文件如下所示:
var transformTabs =
{“tag”:“div”,“id”:“$ {tabname}”,“class”:“tabContent”,“children”:[
{“tag”:“div”,“class”:“datagroup”,“children”:[
{“tag”:“div”,“id”:“$ {dataname}”,“class”:“dataContent”,“children”:[
{“tag”:“div”,“class”:“data”,“children”:[
{“tag”:“p”,“html”:“$ {dataname} - $ {datavalue}”}
]} ]} ]} ]}
由于某种原因,dataname和datavalue没有被转换 - 这些值应出现的地方没有出现任何内容。我用你的构建器来创建变换。你能告诉我我错过了什么吗?
这是我在JSON编码之前的原始数据:
Array
(
[0] => Array
(
[tabname] => orders
[datagroups] => Array
(
[0] => Array
(
[dataname] => ordersToday
[datavalue] => 17
)
[1] => Array
(
[dataname] => orders30Days
[datavalue] => 43
)
)
)
[1] => Array
(
[tabname] => second
[datagroups] => Array
(
[0] => Array
(
[dataname] => Test
[datavalue] => 17
)
)
)
)
答案 0 :(得分:1)
首先,当使用json2html从json转换为html时,您需要记住,转换只是查看数据数组中的顶级对象,而不是解析子数组。
[{"tabname":"orders","datagroups":[]},{"tabname":"second","datagroups":[]}]
所以json2html不知道$ {dataname}和$ {datavalue}存储在数据组中。
也许尝试将json输出重构为此
[{"tabname":"orders","dataname":"test1","datavalue":5},{"tabname":"second","dataname":"test2","datavalue":6}]
或者您可以尝试使用解析子数据组数组的转换,如此
var transforms = {
'tab':{"tag":"div","id":"${tabname}","class":"tabContent","children":function() {
return(json2html.transform(this.datagroups,transforms.group));
}},
'group':{ "tag":"div", "class":"datagroup","children":[
{"tag":"div","id":"${dataname}","class":"dataContent","children":[
{ "tag":"div", "class":"data","children":[
{"tag":"p","html":"${.dataname}-${datavalue}"}
]}
]}
]}
};