0

不显示“address_Components”的所有索引的所有“long_name”值。使用路径时:path("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+);
      }
 }

输出:

登录名称:芝加哥

通过:test_08

4

1 回答 1

0

这是因为您也没有为组件提供索引,这也是一个 json 数组!所以这个通过了,因为你给组件一个索引:

path("results[0].address_components[0].long_name");

虽然在您的描述顶部的粗体文本中,但您没有。

现在要遍历 address_components 数组的各种元素,您需要做的就是找到 address_components 数组的长度,然后将索引从零增加到 <length.of 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++;
            }           
}

这是您的 json 对象的外观(目前它有 4 个要检索的条目)在此处输入图像描述

希望这有助于解决!祝你好运!

于 2017-04-18T12:51:11.280 回答