Skip to content
Advertisement

How to load json data in java?

I’m quite new to API testing, I am wondering how to best and simple load some body? I created simple pojo classes, but i am having problems with nested json. ex:

{
    "listOfItems": [
        {
            "name": "name1",
            "value": "Jack"
        },
        {
            "name": "nameDate",
            "value": "20-08-2021-08-00-00"
        },
        {
            "name": "address",
            "value": "address here",
        }
        {
            "name": "name2",
            "value": "Smith"
        }
    ],
    "something": [],
    "size": 1
}

Then, in classes I used:

ClassName {
private List<ListOfItems> listOfItems;
private List<something> something;
private int size;

//setters and getters
}

and

Class ListOfItems{
private String name;
private String value;

//getters and setters
}

then in test class I am trying to use it, but have no idea how.

public Class Test {

 ClassName className = new ClassName();
ListOfItems list = new ListOfItems();
//how to get list with 3x name and 3x value like in json?

    className.setsize(150);

given().when().body(???).post("endpoint").then()...

}

But I have no idea how to declare those 4 properties (name, value)

Advertisement

Answer

You are actually on a pretty good track, you can use Gson library to help you out, Here is the video example for your explanation which I used to learn Gson back when I needed it

Advertisement