0

我有如下所示的 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":"数据组","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
                        )
                )
    )

)
4

1 回答 1

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}"}
                    ]}
                ]}
        ]}

};

用于工作示例的 JS Fiddle

于 2013-07-04T18:27:47.553 回答