aman
aman

Reputation: 77

how to parse JSON response after i have made a POJO class for JSON response?

This is my POJO class in which i want to parse my JSON.

public class TestPojo {
  @SerializedName("Login Response")
  private List<com.example.amans.demoparsing.LoginResponse> mLoginResponse;

  public List<com.example.amans.demoparsing.LoginResponse> getLoginResponse() {
     return mLoginResponse;
  }

  public void setLoginResponse(List<com.example.amans.demoparsing.LoginResponse> LoginResponse) {
    mLoginResponse = LoginResponse;
  }
}

Upvotes: 0

Views: 1227

Answers (2)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

create your pojo like below and add this gradle in your build.gradle compile 'com.google.code.gson:gson:2.7'

import java.util.List;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TestPojo {

    @SerializedName("Login Response")
    @Expose
    private List<LoginResponse> loginResponse = null;

    public List<LoginResponse> getLoginResponse() {
        return loginResponse;
    }

    public void setLoginResponse(List<LoginResponse> loginResponse) {
        this.loginResponse = loginResponse;
    }


    public class LoginResponse {

        @SerializedName("Status code")
        @Expose
        private String statusCode;
        @SerializedName("OP Status")
        @Expose
        private String oPStatus;
        @SerializedName("Status Message")
        @Expose
        private String statusMessage;
        @SerializedName("Error Message")
        @Expose
        private String errorMessage;
        @SerializedName("Date and Time")
        @Expose
        private String dateAndTime;

        public String getStatusCode() {
            return statusCode;
        }

        public void setStatusCode(String statusCode) {
            this.statusCode = statusCode;
        }

        public String getOPStatus() {
            return oPStatus;
        }

        public void setOPStatus(String oPStatus) {
            this.oPStatus = oPStatus;
        }

        public String getStatusMessage() {
            return statusMessage;
        }

        public void setStatusMessage(String statusMessage) {
            this.statusMessage = statusMessage;
        }

        public String getErrorMessage() {
            return errorMessage;
        }

        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }

        public String getDateAndTime() {
            return dateAndTime;
        }

        public void setDateAndTime(String dateAndTime) {
            this.dateAndTime = dateAndTime;
        }

    }
}

and then use above pojo below like

TestPojo testPojo = new Gson().fromJson(response,TestPojo.class);
String statusMessage = testPojo.getLoginResponse.get(0).getStatusMessage();

Upvotes: 0

Sanoop Surendran
Sanoop Surendran

Reputation: 3486

You need to use GSON class for this

  compile 'com.google.code.gson:gson:2.7'

Then in the code where you the response string add this lines for code

  String response = // your response string is in array
  List<LoginResponse> list = Arrays.asList(new Gson()
                              .fromJson(response, LoginResponse[].class));

OR you want it on your test pojo class

    String response = // your response string
    TestPojo testPojo = new Gson().fromJson(response, TestPojo.class);

Happy coding.. :)

Upvotes: 2

Related Questions