简体   繁体   中英

How to write a model class for json string response in retrofit?

Hi I am new to android,

I have worked with regular json object response but now I have been asked to work with json string response like this,

"{\"message\":\"OTP is sent to your Mobile number and Email id\",\"status\":true,\"existing_user\":true}"

I have no idea how to call API's with this response. I searched how to do it and found nothing. Please help me if anyone knows how to get it done. Thanks in advance!

First, for network calls you can use Retrofit.

Then you can convert your JSON response to java class with this site Json to java

For example, your JSON is converted to the below class

public class Output{

public String message;
public Boolean status;
public Boolean existingUser;

}
package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Example {

@SerializedName("message")
@Expose
private String message;

@SerializedName("status")
@Expose
private Boolean status;

@SerializedName("existing_user")
@Expose
private Boolean existingUser;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public Boolean getExistingUser() {
return existingUser;
}

public void setExistingUser(Boolean existingUser) {
this.existingUser = existingUser;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM