如何在改造中为 json 字符串响应编写模型 class?

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

嗨,我是 android、

的新手

我曾使用常规 json 对象响应,但现在我被要求使用这样的 json 字符串响应,

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

我不知道如何用这个响应调用 API。我搜索了如何做,但一无所获。如果有人知道如何完成,请帮助我。 提前致谢!

首先,对于网络调用,您可以使用 Retrofit。

然后您可以使用此网站将您的 JSON 回复转换为 java class Json to java

比如你的JSON转换成下面的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;
}

}