Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 编辑后未保存存储库中的本地文件(groovy/jenkins)_Json_Jenkins_Groovy - Fatal编程技术网

Json 编辑后未保存存储库中的本地文件(groovy/jenkins)

Json 编辑后未保存存储库中的本地文件(groovy/jenkins),json,jenkins,groovy,Json,Jenkins,Groovy,在詹金斯的工作中,我查看了一个存储库。在该repo中,有一个文件我想在作业期间编辑。但文件似乎没有保存。我有这样一种方法: def updateFile(id, key){ def inputFile = readFile("${workspace}/config/cnf.json") def inputJSON = new JsonSlurper().parseText(inputFile) inputJSON."${key}"[0].pref = "${id}"

在詹金斯的工作中,我查看了一个存储库。在该repo中,有一个文件我想在作业期间编辑。但文件似乎没有保存。我有这样一种方法:

def updateFile(id, key){
    def inputFile = readFile("${workspace}/config/cnf.json")
    def inputJSON = new JsonSlurper().parseText(inputFile)

    inputJSON."${key}"[0].pref = "${id}"
    def result = JsonOutput.toJson(inputJSON)
    //here it is changed.
    println "result:\n${result}"
    inputFile << "${JsonOutput.prettyPrint(result)}"
    //and now it is again the old one.
    println "Hier: \n ${inputFile}"
}
writeFile file:"${workspace}/config/cnf.json", text:result
有没有保存现有文件的好方法

if
readFile(“${workspace}/config/cnf.json”)
工作正常

然后,要编写文件,请使用以下方法:

def updateFile(id, key){
    def inputFile = readFile("${workspace}/config/cnf.json")
    def inputJSON = new JsonSlurper().parseText(inputFile)

    inputJSON."${key}"[0].pref = "${id}"
    def result = JsonOutput.toJson(inputJSON)
    //here it is changed.
    println "result:\n${result}"
    inputFile << "${JsonOutput.prettyPrint(result)}"
    //and now it is again the old one.
    println "Hier: \n ${inputFile}"
}
writeFile file:"${workspace}/config/cnf.json", text:result

谢谢你的帮助。我测试过了。但是我得到了错误:java.io.NotSerializableException:groovy.json.internal.LazyMap:-(使用
JsonSlurperClassic
而不是
JsonSlurper
。您只能在jenkins管道中使用可序列化对象。JsonSlurperClassic返回正常的
java.util.HashMap
而不是
LazyMap