简体   繁体   English

Groovy MarkupBuilder通过引用传递字符串

[英]Groovy MarkupBuilder passing string by reference

Doing transformation of a JSON feed to a format that can be consumed by our Endeca Instance, and settled on writing this transformation in Groovy, due to tools like JsonSlurper and MarkupBuilder. 由于JsonSlurper和MarkupBuilder之类的工具,将JSON feed转换为Endeca实例可以使用的格式,并决定在Groovy中编写此转换。 Our JSON feed input looks like this (saved as stage/newObject.json): 我们的JSON feed输入看起来像这样(保存为stage / newObject.json):

[
  {
    "Name": "Object Name",
    "DimID": 0000,
    "ObjectID": "Object-0000",
    "TypeID": 1,
    "Type": "Object Type",
    "Description": "A Description",
    "DirectorID": "007",
    "DirectorName": "Bond, James",
    "ParentObjectID": null,
    "ObjectLevelID": 1,
    "ObjectLevel": "MI-6",
    "ProjectNumbers": [
        "0001",
        "0002"
    ],
    "ManagerIDs": [
      "001"
    ],
    "ManagerNames": [
      "M"
    ],
    "SubObjectIDs": [
        "006",
        "005"
    ],
    "TaskNumbers": [
        "001"
    ],
    "ProjectNames": [
        "Casino Royal",
        "Goldfinger",
        "License to Kill"
    ]
  }
]

And the code I've got to do the transform is this: 我要做转换的代码是这样的:

def rawJSONFile = new File("stage/newObject.json")
JsonSlurper slurper = new JsonSlurper()
def slurpedJSON = slurper.parseText(rawJSONFile.text)

def xmlOutput = new MarkupBuilder(new FileWriter(new File("stage/ProcessedOutput.xml")))

xmlOutput.RECORDS() {
    for (object in slurpedJSON) {
        RECORD() {
            for (field in object) {
                if (field.value != null) {
                    if (field.value.value.class.equals(java.util.ArrayList)) {
                        if (field.value.value.size > 0) {
                            for (subField in field.value.value) {
                                if (subField != null) {
                                    PROP(NAME: field.key) {
                                        PVAL(subField)
                                    }
                                }
                            }
                        }
                    } else {
                        PROP(NAME: field.key) {
                            PVAL(field.value)
                        }
                    }
                }
            }
        }
    }
}

The issue we're experiencing is about half way down the groovy script, where it's dealing with sub fields (that is the arrays within the JSON), the closure that's creating the "PVAL" node is passing the subField variable by reference and it's not being treated as a string but a character array, so trying to do the output, we get a Memory location, rather than a String. 我们遇到的问题大约在groovy脚本的一半位置,它正在处理子字段(即JSON中的数组),创建“ PVAL”节点的闭包正在通过引用传递subField变量,而不是被当作​​字符串而是字符数组对待,因此尝试执行输出,我们得到的是内存位置,而不是字符串。 The workaround we've got so far is this, but I wanted to know if there was a more elegant solution: 到目前为止,我们已经解决了这个问题,但是我想知道是否有一个更优雅的解决方案:

for (subField in field.value.value) {
    if (subField != null) {
        PROP(NAME: field.key) {
            StringBuilder subFieldValue = new StringBuilder();
            for(int i =0; i<subField.length; i++){
                subFieldValue.append(subField[i])
            }
            PVAL(subFieldValue.toString())
        }
    }
}

Change subField in field.value.value to subField in field.value in subField in field.value.value中的subField in field.value更改subField in field.value.value中的subField in field.value

for (subField in field.value) {
    if (subField != null) {
        PROP(NAME: field.key) {
            PVAL(subField)
        }
    }
}

Although this logic can be simplified as 尽管此逻辑可以简化为

xmlOutput.RECORDS {
    slurpedJSON.each { map ->
        Record {
            map.each { k, v ->
                if ( v in ArrayList ) {
                    v.each { val ->
                        PROP(NAME: k) { 
                            PVAL(val)
                        }
                    }
                } else {
                    PROP(NAME: k) { 
                        PVAL(v)
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM