I roto i tenei akoranga Whakaaturanga API, ka tirohia me pehea te tango i te urupare a te JSON me te tango korero ma te whakamahi i te wharepukapuka REST.
I a koe e whakamatautau ana i te API, ka tono koe i tetahi rauemi, (hei tauira ma te tono GET, mo te POST ranei). Ka hoki mai te kaituku me te whakautu ka mahi koe i etahi korero mo te whakautu.
Mo tenei akoranga, ka whakamahi ahau Tuhinga o mua he API REST rūpahu ā-ipurangi mō te Whakamātautau me te Tauira. Ko te JSONPlaceholder he ratonga REST ipurangi koreutu, ka taea e koe te whakamahi i nga wa katoa e hiahia ana koe ki nga korero rūpahu.
Ko te mea nui ake, ka whakamahi ahau i te tohu mutunga o nga kaiwhakamahi jsonplaceholder .
Ka tono ana maatau ki te rauemi o runga ake nei, ka tae mai he whakautu ki a JSON kei roto he raarangi kaiwhakamahi. Ko tenei raarangi ko te JSON Array. He penei te hanganga o ia tatai:
{
id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: 'Sincere@april.biz',
address: {
street: 'Kulas Light',
suite: 'Apt. 556',
city: 'Gwenborough',
zipcode: '92998-3874',
geo: {
lat: '-37.3159',
lng: '81.1496'
}
},
phone: '1-770-736-8031 x56442',
website: 'hildegard.org',
company: {
name: 'Romaguera-Crona',
catchPhrase: 'Multi-layered client-server neural-net',
bs: 'harness real-time e-markets'
} }
No reira, i roto i te whakautu katoa, ka tekau nga rekoata i te raupapa, he rite te hanganga o te JSON, engari he rereke nga uara.
E pā ana:
Na, kia tiimata ma te wehe me te tango i etahi uara mai i te JSON.
Ko te whakamatautau tuatahi ko te tatau i te maha o nga rekoata kei te kohinga, no reira me tiimata.
import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.parsing.Parser; import io.restassured.response.Response; import java.util.List; import static io.restassured.RestAssured.given; public class RestTest {
public static Response doGetRequest(String endpoint) {
RestAssured.defaultParser = Parser.JSON;
return
given().headers('Content-Type', ContentType.JSON, 'Accept', ContentType.JSON).
when().get(endpoint).
then().contentType(ContentType.JSON).extract().response();
}
public static void main(String[] args) {
Response response = doGetRequest('https://jsonplaceholder.typicode.com/users');
List jsonResponse = response.jsonPath().getList('$');
System.out.println(jsonResponse.size());
} }
Ko te hua o te karanga i runga ake nei ka taarua 10
. Kia mahara ki te $
tuhinga e kiia nei ko te putake putake.
I te tauira i runga ake nei, mena e hiahia ana matou ki te tiki i te ingoa kaiwhakamahi o nga urunga katoa, ka taea e matou te whakamahi:
String usernames = response.jsonPath().getString('username'); System.out.println(usernames);
Ma tenei ka taarua te raupapa penei:
[Bret, Antonette, Samantha, Karianne, Kamren, Leopoldo_Corkery, Elwyn.Skiles, Maxime_Nienow, Delphine, Moriah.Stanton]
Mena ka hiahia maatau ki te tiki i te ingoa kaiwhakamahi o te urunga tuatahi ka taea e taatau te whakamahi:
String usernames = response.jsonPath().getString('username[0]');
Ma tenei ka taarua te ingoa tuatahi:
Bret
Ma te whakamahi i te Rarangi ka taea e taatau te whakamahi:
List jsonResponse = response.jsonPath().getList('username'); System.out.println(jsonResponse.get(0));
Ma tenei ka taarua te ingoa tuatahi:
Bret
I te titiro ki te hanganga JSON o runga ake nei, he mahere tonu te kamupene. Mena kotahi noa o taatau rekoata, ka taea e maatau te whakamahi:
Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/1'); Map company = response.jsonPath().getMap('company'); System.out.println(company.get('name'));
e taia:
Romaguera-Crona
Engari mena ka whakahoki te urupare i te ngohi me te hiahia ki te tango i te ingoa o te kamupene tuatahi, ka taea e maatau te whakamahi:
Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/'); Map company = response.jsonPath().getMap('company[0]'); System.out.println(company.get('name'));
Ano hoki, ka taea e taatau te whakamahi:
Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/'); List
e rua o nei ka ta:
Romaguera-Crona