本文整理汇总了Java中javax.json.JsonArray.getValuesAs方法的典型用法代码示例。如果您正苦于以下问题:Java JsonArray.getValuesAs方法的具体用法?Java JsonArray.getValuesAs怎么用?Java JsonArray.getValuesAs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.json.JsonArray
的用法示例。
在下文中一共展示了JsonArray.getValuesAs方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGet
import javax.json.JsonArray; //导入方法依赖的package包/类
private void testGet(JsonPClient client, String clientType) {
reset();
stubFor(get(urlEqualTo("/"))
.willReturn(aResponse()
.withBody("[{\"key\": \"value\"}, {\"key\": \"anotherValue\"}]")));
JsonArray jsonArray = client.get();
assertEquals(jsonArray.size(), 2, "Expected 2 values in the array for client "+clientType);
List<JsonObject> jsonObjects = jsonArray.getValuesAs(JsonObject.class);
JsonObject one = jsonObjects.get(0);
assertEquals(one.keySet().size(), 1, "There should only be one key in object 1 for client "+clientType);
assertEquals(one.getString("key"), "value", "The value of 'key' on object 1 should be 'value' in client "+clientType);
JsonObject two = jsonObjects.get(1);
assertEquals(two.keySet().size(), 1, "There should only be one key in object 2 for client "+clientType);
assertEquals(two.getString("key"), "anotherValue", "The value of 'key' on object 2 should be 'anotherValue' in client "+clientType);
}
示例2: parseAddress
import javax.json.JsonArray; //导入方法依赖的package包/类
@Override
public Address parseAddress(JsonObject json) {
JsonArray results = json.getJsonArray("results");
if (!results.isEmpty()) {
Address address = new Address();
JsonObject result = (JsonObject) results.get(0);
JsonArray components = result.getJsonArray("address_components");
for (JsonObject component : components.getValuesAs(JsonObject.class)) {
String value = component.getString("short_name");
typesLoop: for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
switch (type.getString()) {
case "street_number":
address.setHouse(value);
break typesLoop;
case "route":
address.setStreet(value);
break typesLoop;
case "locality":
address.setSettlement(value);
break typesLoop;
case "administrative_area_level_2":
address.setDistrict(value);
break typesLoop;
case "administrative_area_level_1":
address.setState(value);
break typesLoop;
case "country":
address.setCountry(value);
break typesLoop;
case "postal_code":
address.setPostcode(value);
break typesLoop;
default:
break;
}
}
}
return address;
}
return null;
}