Reputation: 159
I need to send following json request from android app. im using retrofit 2. How to create a pojo class to send this kind of json request?
{
"json_data": {
"firstname": "fname",
"lastname": "lname",
"email": "[email protected]",
"telephone": "0123456789",
"salutation": "Mr.",
"dob": "1997/08/15",
"password":"123456"
}
}
Upvotes: 0
Views: 1280
Reputation: 1
Use the following way, your left side fields should the variable name in POJO.
Java :
class Data {
public String firstname;
public String lastname;
public String email;
public String telephonies;
public String salutation;
public String dob;
public String password;
}
class Request{
Data json_data;
}
Upvotes: 0
Reputation: 424
I hope you are asking how to make a POJO from JSON
You can try using JSON to java converter here https://codebeautify.org/json-to-java-converter Just Paste your JSON template and hit convert, You will have POJO class with all the getters and setter.
There is also an inbuilt plugin you can use. try this https://plugins.jetbrains.com/plugin/8533-json2pojo
Upvotes: 1
Reputation: 11467
Try inbuilt plugins to convert json to POJO class like this
Go to android studio settings
Go to Plugin section
Install this plugin for java
Copy json
Click on package in which you want to create class
Click on new then choose json to POJO like this
Upvotes: 0
Reputation: 403
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("json_data")
@Expose
private JsonData jsonData;
public JsonData getJsonData() {
return jsonData;
}
public void setJsonData(JsonData jsonData) {
this.jsonData = jsonData;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class JsonData {
@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("lastname")
@Expose
private String lastname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("telephone")
@Expose
private String telephone;
@SerializedName("salutation")
@Expose
private String salutation;
@SerializedName("dob")
@Expose
private String dob;
@SerializedName("password")
@Expose
private String password;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getSalutation() {
return salutation;
}
public void setSalutation(String salutation) {
this.salutation = salutation;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
this site create these for you
Upvotes: 0