は、私が「jsonFieldName」またGroovyを使用してJSON応答から値を抽出するには変数を使用する方法はありますか?
という変数に格納された値の場所を使用してJSONレスポンスから自転車ブランド「キャノンデール」を抽出しようとしている、私は正常に使用してブランド価値を抽出することができますよ次の構文を使用します。
def brand = json.store.bicycle.brand
ただし、要素の場所を変数に保存したいと考えています。理由は、私はJson Responseで複数のアサーションをオートメーションスイートの一部として反復できるようにしたいということです。
これを行う方法を親切にアドバイスできますか?
以下は、私が現在場所に変数を格納しているスニペットです。しかし、そのが働いて、常に「NULL」としてブランドを返していない:( おかげ
def response = ('''{
"store": {
"book": [
{
"title": "Sword of Honour",
"category": "fiction",
"author": "Evelyn Waugh",
"@price": 12.99
},
{
"title": "Moby Dick",
"category": "fiction",
"author": "Herman Melville",
"isbn": "0-553-21311-3",
"@price": 8.99
},
{
"title": "Sayings of the Century",
"category": "reference",
"author": "Nigel Rees",
"@price": 8.95
},
{
"title": "The Lord of the Rings",
"category": "fiction",
"author": "J. R. R. Tolkien",
"isbn": "0-395-19395-8",
"@price": 22.99
}
],
"bicycle": {
"brand": "Cannondale",
"color": "red",
"price": 19.95
}
}
}''').toString()
//store location of json property I want to extract in property called jsonFieldName
def jsonFieldName = "store.bicycle.brand"
def json = new JsonSlurper().parseText (response)
//perform extraction
brand = json."${jsonFieldName}"
が実際より機能的なアプローチは、あなたの答えは素敵な1を作ります! – albciff