2

我想将 XML 文件转换为 JSON。问题是,我有结构

<node id="1">
    <title>Content ...</title>
</node>

<node id="2">
    <title>Secon ...</title>
    <subnodes>
        <node id="3">
            <title>Secon ...</title>
            <subnodes>
                <node id="4">
                    <title>Secon ...</title>
                </node>
            </subnodes>
        </node>
    </subnodes>
</node>

我希望它为 JSON 格式,例如:

{
  "nodeid": "34",
  "text": "First level",
  "children": [{
        "nodeid": "1",
        "text": "Content ...",
        "leaf": true,
        "children": [{
              "nodeid": "2",
              "text": "Second",
              "leaf": true,
              "children": [{
                "nodeid": "3",
                "text": "Third",
                "leaf": true
              } ** , ** ]

但是在laste children 之后总是有一个逗号“,”。使用 freemarker 可以确定节点是否有父节点、子节点或其他任何节点,例如节点?父节点、节点?子节点。但是没有机会找出它是否有兄弟姐妹。

freemarker 如何知道当前节点是否有兄弟节点?

4

1 回答 1

1

列表循环中有两个特殊的循环变量可用:

item_index:这是一个数值,包含在循环中被跨步的当前项目的索引。

item_has_next:布尔值,指示当前项目是否是序列中的最后一个。

例子:

<#assign seq = ["winter", "spring", "summer", "autumn"]>
<#list seq as x>
  ${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>

http://freemarker.sourceforge.net/docs/ref_directive_list.html

于 2011-03-10T13:29:26.487 回答