Shanu
Shanu

Reputation: 159

How to create necessary pojo classes to send nested json data

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

Answers (4)

Android Sri
Android Sri

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

Gouse Mohiddin
Gouse Mohiddin

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

Quick learner
Quick learner

Reputation: 11467

Try inbuilt plugins to convert json to POJO class like this

  1. Go to android studio settings

  2. Go to Plugin section

  3. Install this plugin for java

enter image description here

enter image description here

  1. Install this plugin for kotlin

enter image description here

  1. Copy json

  2. Click on package in which you want to create class

  3. Click on new then choose json to POJO like this

enter image description here

Upvotes: 0

Shahriyar Aghajani
Shahriyar Aghajani

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

enter image description here

Upvotes: 0

Related Questions