How to bind the json
file to object.
Here I am using JSR-367 API , yasson implementation to implement.
The JSON file looks like this
{
"Details": [
{
"age": 27,
"gender": "Male",
"name": "John"
},
{
"age": 27,
"gender": "Male",
"name": "Max"
},
{
"age": 27,
"gender": "FeMale",
"name": "esh"
}
]
}
For the above json I am created two Binding classes, Those are
This is for the array objects which are presented in the json
.
1.
public class Details
{
private String name;
private int age;
private String gender;
private long phoneNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
and this is for Details array
2)
public class Root
{
private Details[] details;
public Details[] getDetails() {
return members;
}
}
This is the main class for binding the classes from json
file to object
public class TestJsonB
{
static final String JSON_FILE = "/media/Resources/Details.json";
public static void main(String[] args) throws IOException
{
String content = new String(Files.readAllBytes(Paths.get(JSON_FILE)));
Jsonb jsonB = JsonbBuilder.create();
Root root = jsonB.fromJson(content,Root.class);
Details[] details = root.getDetails();
System.out.println(details);//null printing
}
}
Help me to write the binding classes for the given json
file.