Maitiro Ekuparadzanisa JSON Mhinduro uine REST-yakavimbiswa

Mune ino API Yeyedzo Yekudzidzisa dzidziso, isu tinotarisa maitiro ekuparadzanisa mhinduro yeJSON uye kubvisa ruzivo tichishandisa REST-yakavimbiswa raibhurari.

Paunenge uchiyedza API, iwe unowanzo ita chikumbiro kune zviwanikwa, (i.e. kuburikidza neGET kana POST chikumbiro). Seva inodzoka nemhinduro uye wozoita zvirevo pamhinduro.

Maitiro Ekuparadzanisa JSON Mhinduro

Kune ino dzidzo, ini ndichave ndichishandisa JSONPlaceholder inova inhema yepamhepo REST API yeTesting uye Prototyping. JSONPlaceholder ndeye yemahara online REST sevhisi iwe yaunogona kushandisa pese paunoda imwe nhema data.

Zvikurukuru, ini ndichave ndichishandisa iyo endpoint yekushandisa jsonplaceholder .

Kumbira uye Kupindura

Patinoita chikumbiro cheGET kuchinhu chiri pamusoro, tinowana mhinduro yeJSON ine runyorwa rwevashandisi. Urongwa urwu hunomiririrwa seJSON Array. Imwe neimwe rondedzero ine chimiro chakadai.

{
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'
} }

Naizvozvo, mumhinduro izere, pachave nezvinyorwa gumi mumutsara, imwe neimwe iine chimiro chakafanana cheJSON, asi iine akasiyana maitiro.



Inoenderana:

Zvino, ngatitangei nekuparadzanisa uye kubvisa mamwe maitiro kubva kuJSON.

Muedzo wekutanga waizowanzove wekuverenga iyo nhamba yezvinyorwa mune zvakarongeka, saka ngatitange nazvo.

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());
} }

Mhedzisiro yekufona iri pamusoro yaizodhinda 10. Cherekedza iyo $ notation izvo zvinoreva mudzi chinhu.

Kuparadzanisa JSON Arrays uye Lists

Mumuenzaniso wepamusoro, kana isu tichida kutora zita rekushandisa rezvinyorwa zvese, tinogona kushandisa:

String usernames = response.jsonPath().getString('username'); System.out.println(usernames);

Izvi zvaizoprinta rondedzero senge:

[Bret, Antonette, Samantha, Karianne, Kamren, Leopoldo_Corkery, Elwyn.Skiles, Maxime_Nienow, Delphine, Moriah.Stanton]

Kana isu tikazoda kutora zita rekushandisa rekupinda kwekutanga kwatinogona kushandisa:

String usernames = response.jsonPath().getString('username[0]');

Izvi zvaizodhinda zita rekutanga rekushandisa:

Bret

Tichishandisa Rondedzero yatinogona kushandisa:

List jsonResponse = response.jsonPath().getList('username'); System.out.println(jsonResponse.get(0));

Izvi zvaizodhinda zita rekutanga rekushandisa:

Bret

Kubvisa JSON ArrayList uye HashMap

Tichitarisa pamusoro pechimiro chiri pamusoro cheJSON, kambani iri chaizvo mepu. Dai isu chete taive nechinyorwa chimwe chete, taigona kushandisa:

Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/1'); Map company = response.jsonPath().getMap('company'); System.out.println(company.get('name'));

iyo yaizodhinda:

Romaguera-Crona

Asi kana mhinduro ikadzosa rondedzero uye isu tichida kutora zita rekambani rekutanga, tinogona kushandisa:

Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/'); Map company = response.jsonPath().getMap('company[0]'); System.out.println(company.get('name'));

Neimwe nzira, isu tinogona kushandisa:

Response response = doGetRequest('https://jsonplaceholder.typicode.com/users/'); List companies = response.jsonPath().getList('company'); System.out.println(companies.get(0).get('name'));

ese ari maviri anodhinda:

Romaguera-Crona