2016-12-15 7 views
1

私のリターンJSONは次のようになります無名配列である:これに基づきアレイ応答の特定のエントリを見つけるにはどうすればいいですか?

[ 
    "name" : "John", 
    "age" : 26, 
    "likes" : "Ice Cream" 
], 
[ 
    "name" : "Jake", 
    "age" : 26, 
    "likes" : "Pizza" 
] 

私は年齢が26であると同類は「ピザ」(いわゆる「ジェイク」であるエントリの名前を取得したいですこの場合)。それ、どうやったら出来るの?

これまでのところ私はこれを持っていますが、hasItemは2つのアイテムが同じコレクション内にあるかどうかはまったく見ないので、間違っています。また、応答は単に私にすべてを与える。

String name = given().contentType("application/json").when(). 
       get(base + "/persons"). 
       then(). 
       statusCode(200). 
       body("age", hasItem(26)). 
       body("likes", hasItem("Pizza")). 
       extract().response(); 

どうすればよいですか?

JsonPathで
+0

は [{ "名前" にあなたの帰りの構造を変更してみてください"好き": "アイスクリーム" }、 { "名": "ジェイク"、 "年齢":26、 は "好き": "ピザ" }] – Bindrid

+0

申し訳ありませんああ、それは実際にそうでした。このコードで中括弧を忘れてしまった。しかし、正しいエントリーを見つけ出すという問題を解決するわけではありません。 – Selbi

答えて

0

、あなたはこれを試すことができます:https://static.javadoc.io/com.jayway.restassured/rest-assured/1.4/com/jayway/restassured/path/json/JsonPath.html

JsonPathがなければ、あなたがこれを行うことができます:

Response response = given().contentType("application/json").get(base + "/persons"); 
response.then().statusCode(200); 

String name = JsonPath.with(response.getBody()).getString("$[?(@.age == 26 && @.likes eq 'Pizza')].name"); 
Assert.assertEquals("Jake", name); 

を私は確信しているが動作する100%ではないけど、それがされますようなもの

"ジョン"、 "年齢":26、
Response response = given().contentType("application/json")get(base + "/persons"); 
response.then().statusCode(200); 

List<Person> people = Arrays.asList(response.getBody().as(Person[].class)); 
Assert.assertTrue(!people.isEmpty()); 

for(Person person : people) { 
    if(person.getAge() == 26 && person.getLikes().equals("Pizza")) { 
    Assert.assertEquals("Jake", person.getName()); 
    } 
} 
関連する問題