Converting this JSON object as a class in java, how would the mapping be in your POJO Class?
{
\"ownerName\": \"Robert\",
\"pets\": [
{
This kind of question is very popular and needs general answer. In case you need generate POJO
model based on JSON
or JSON Schema
use www.jsonschema2pojo.org. Example print screen shows how to use it:
How to use it:
Java
in your case.JSON
in your case.JSON
. In case schema is simple do not use annotations (None
option).Include getters and setters
. You can do that in your IDE
as well.Preview
button. In case schema is big download ZIP
with generated classes.For your JSON
this tool generates:
public class Person {
private String ownerName;
private List <Pet> pets = null;
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public List < Pet > getPets() {
return pets;
}
public void setPets(List < Pet > pets) {
this.pets = pets;
}
}
public class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
For Android Studio
and Kotlin
read RIP http://www.jsonschema2pojo.org.
In the above json you have ownerName
as property, pets
as List of objects
public class Response {
private String ownerName;
private List<Pet> pets;
// getters and setters
}
Pet POJO
public class Pet {
private String name;
//getters and setters
}
You can use the following classes:
public class MyObject {
private String ownerName;
private List<Pet> pets;
}
public class Pet {
private String name;
}