JSON, öznitelik değeri çiftleri ve dizi veri türlerinden oluşan veri nesnelerini iletmek için insan tarafından okunabilir metin kullanan bir biçimdir. Yani, genel olarak json biçimlendirilmiş bir metindir.
Yüce json nesnesinde yalnızca bir dizi harita/dizi var.
node{
def data = readJSON file:'message2.json'
echo "color: ${data.attachments[0].color}"
}
bina json kodundan boru hattı kullanarak json ayrıştırma JsonSlurperClassic
//use JsonSlurperClassic because it produces HashMap that could be serialized by pipeline
import groovy.json.JsonSlurperClassic
node{
def json = readFile(file:'message2.json')
def data = new JsonSlurperClassic().parseText(json)
echo "color: ${data.attachments[0].color}"
}
kullanarak json ayrıştırma ve bunu dosyaya yazma
import groovy.json.JsonOutput
node{
//to create json declare a sequence of maps/arrays in groovy
//here is the data according to your sample
def data = [
attachments:[
[
fallback: "New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",
pretext : "New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",
color : "#D00000",
fields :[
[
title: "Notes",
value: "This is much easier than I thought it would be.",
short: false
]
]
]
]
]
//two alternatives to write
//native pipeline step:
writeJSON(file: 'message1.json', json: data)
//but if writeJSON not supported by your version:
//convert maps/arrays to json formatted string
def json = JsonOutput.toJson(data)
//if you need pretty print (multiline) json
json = JsonOutput.prettyPrint(json)
//put string into the file:
writeFile(file:'message2.json', text: json)
}
, ayrıştırma hakkında sorduğunuz soru başlığında ve söz konusu sorunun kendisinde json dosyası oluşturmayı soruyorsunuz. ne istediğini açıklığa kavuşturur musun, yapmaya çalış. – daggett
@daggett Bu JSON nesnesini groovy değişkenine oluşturmak istiyorum. –