1
我只是兩天歲,需要解析具有以下結構的json文件。我實際的想法是,我需要運行一組基於不同的順序不同環境的工作,所以我想出了JSON作爲輸入文件的格式,以我的常規使用groovy腳本解析JSON(使用JsonSlurper)
{
"services": [{
"UI-Service": [{
"file-location": "/in/my/server/location",
"script-names": "daily-batch,weekly-batch,bi-weekly-batch",
"seq1": "daily-batch,weekly-batch",
"seq2": "daily-batch,weekly-batch,bi-weekly-batch",
"DEST-ENVT_seq1": ["DEV1", "DEV2", "QA1", "QA2"],
"DEST-ENVT_seq2": ["DEV3", "DEV4", "QA3", "QA4"]
}]
}, {
"Mobile-Service": [{
"file-location": "/in/my/server/location",
"script-names": "daily-batch,weekly-batch,bi-weekly-batch",
"seq1": "daily-batch,weekly-batch",
"seq2": "daily-batch,weekly-batch,bi-weekly-batch",
"DEST-ENVT_seq1": ["DEV1", "DEV2", "QA1", "QA2"],
"DEST-ENVT_seq2": ["DEV3", "DEV4", "QA3", "QA4"]
}]
}]
}
我嘗試以下腳本解析JSON
def jsonSlurper = new JsonSlurper()
//def reader = new BufferedReader(new InputStreamReader(new FileInputStream("in/my/location/config.json"),"UTF-8"))
//def data = jsonSlurper.parse(reader)
File file = new File("in/my/location/config.json")
def data = jsonSlurper.parse(file)
try{
Map jsonResult = (Map) data;
Map compService = (Map) jsonResult.get("services");
String name = (String) compService.get("UI-Service");
assert name.equals("file-location");
}catch (E){
println Exception
}
我需要首先讀所有服務(UI服務,移動設備服務,等等。)然後它們的元素和它們的用於從對象JsonParser讀取值
基於我需要檢索數據,#1 - 需要找到屬於哪個DEST-ENVT_seq組(例如:DEST-ENVT_seq2),並且從用戶獲得只有envt名稱(例如:DEV2或QA3)然後根據我需要fi的組名nd seq(例如:seq2)並從文件位置複製這些文件並執行 – Mowgli