Sida Looga Jawaabo JSON Jawaab leh REST-la hubo

Casharradan Tijaabinta API, waxaan eegeynaa sida loo falanqeeyo jawaabta JSON loona soo saaro macluumaadka iyadoo la adeegsanayo maktabadda lagu kalsoon yahay ee 'REST'.

Markaad tijaabinaysid API, waxaad caadi ahaan codsi u gudbisaa ilaha, (tusaale ahaan adoo adeegsanaya GET ama POST codsi). Adeeggu wuxuu ku soo laabanayaa jawaab ka dibna waxaad ku xaqiijineysaa jawaabta.

Sidee Loo Sifeeyaa Jawaabta JSON

Casharkaan, waan isticmaali doonaa JSONPlaceholder taas oo ah been abuur REST API ah oo loogu talagalay tijaabinta iyo kahortaga. JSONPlaceholder waa adeeg REST oo bilaash ah oo khadka tooska ah waxaad isticmaali kartaa markasta oo aad ubaahantahay xog been abuur ah.

Si gaar ah, waxaan isticmaali doonaa dhamaadka isticmaalayaasha jasonplaceholder .

Codsi iyo Jawaab

Markii aan codsi ka helno GET ilaha kor ku xusan, waxaan helnaa jawaabta JSON oo ay ku jiraan liiska isticmaalayaasha. Liiskan waxaa loo matalay sidii JSON Array. Qeyb kastaa waxay leedahay qaab dhismeed sidan ah:

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

Sidaa darteed, jawaabta oo buuxda, waxaa jiri doona toban diiwaan oo saf ah, mid walbana wuxuu leeyahay qaab dhismeedka JSON oo isku mid ah, laakiin leh qiyam kala duwan.



La xiriira:

Hadda, aan ku bilowno baaritaanka iyo ka soo saarista qaar ka mid ah qiimayaasha JSON.

Imtixaanka ugu horreeya wuxuu caadi ahaan ahaan lahaa in la tiriyo tirada rikoodhada ee safka, sidaa darteed aan ku bilowno taas.

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

Natiijada wicitaanka kor ku xusan wuxuu daabacayaa 10. U fiirso $ farriin taas oo macnaheedu yahay cunsurka aasaasiga ah.

Kala bixinta JSON Arrays iyo Liisaska

Tusaalaha kore, haddii aan rabno inaan helno magaca isticmaale ee dhammaan waxqabadyada, waxaan isticmaali karnaa:

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

Tani waxay daabacan doontaa qormada sida:

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

Haddii aan markaa dooneyno inaan helno magaca isticmaale ee gelitaanka hore waxaan isticmaali karnaa:

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

Kani wuxuu daabici doonaa magaca isticmaale ee ugu horreeya:

Bret

Isticmaalka Liis waxaan isticmaali karnaa:

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

Kani wuxuu daabici doonaa magaca isticmaale ee ugu horreeya:

Bret

Kala bixinta JSON ArrayList iyo HashMap

Markaad fiirineyso qaab-dhismeedka sare ee JSON, shirkaddu dhab ahaantii waa khariidad. Haddii aan heysanno hal rikoor, waxaan isticmaali karnaa:

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

kaas oo daabici lahaa:

Romaguera-Crona

Laakiin haddii jawaabta ay soo celiso isku xirnaan oo aan dooneyno inaan ka saarno magaca shirkadda ugu horreysa, waxaan isticmaali karnaa:

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

Haddii kale, waxaan isticmaali karnaa:

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

oo labaduba daabici doona:

Romaguera-Crona