不显示“address_Components”的所有索引的所有“long_name”值。使用路径时:路径(“results [0] .address_components []。long_name”) 然后测试用例失败了。 我想显示响应中给出的所有“long_name”值。请建议我。
{
"results" : [
{
"address_components" : [
{
"long_name" : "Chicago",
"short_name" : "Chicago",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Cook County",
"short_name" : "Cook County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Illinois",
"short_name" : "IL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chicago, IL, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.023131,
"lng" : -87.52366099999999
},
"southwest" : {
"lat" : 41.6443349,
"lng" : -87.9402669
}
},
"location" : {
"lat" : 41.8781136,
"lng" : -87.6297982
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 42.023131,
"lng" : -87.52404399999999
},
"southwest" : {
"lat" : 41.6443349,
"lng" : -87.9402669
}
}
},
"place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
我正在使用硒(Maven TestNG):
@Test
public void test_08(){
String[] arrayList = {"chicago"};
for(int i=0; i<arrayList.length; i++){
Response resp = given().
parameter("address", arrayList[i]).
when().
get("http://maps.googleapis.com/maps/api/geocode/json");
Assert.assertEquals(resp.getStatusCode(), 200);
String respReport = resp.
then().
contentType(ContentType.JSON).
extract().
path("results[0].address_components[0].long_name");
System.out.println("Logn Name: "+respReport+);
}
}
登录名:芝加哥
PASSED:test_08
答案 0 :(得分:0)
这是因为你也没有给组件索引,这也是一个json数组!所以这个通过,因为你给组件索引:
path("results[0].address_components[0].long_name");
虽然在您的描述顶部的粗体文字中,但您不会。
现在,为了遍历address_componenents数组的各种元素,您需要做的就是找到address_components数组的长度,然后将索引从零增加到&lt; json数组的长度。像这样:
String jsonObjectAsString = jsonObjectFromYourResponse.toString();
public void retrieveAddressComponenets(String jsonObjectAsString){
JsonElement jelement = new JsonParser().parse(jsonObjectAsString);
JsonObject jobject = jelement.getAsJsonObject();
JsonArray jArray = jobject.getAsJsonArray("address_components");
int i=0;
while(i<jArray.length()){
jelement = new JsonParser().parse(jarray.get(i).toString());
System.out.println("Logn Name: "+respReport);
i++;
}
}
希望这有助于解决! 祝你好运!