简体   繁体   English

如何在 retrofit 中为 json 字符串响应编写 model class?

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

Hi I am new to android,嗨,我是 android 的新手,

I have worked with regular json object response but now I have been asked to work with json string response like this,我曾使用常规的 json object 响应,但现在我被要求使用 json 字符串响应,如下所示,

"{\"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.我不知道如何用这个响应调用 API。 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.首先,对于网络调用,您可以使用 Retrofit。

Then you can convert your JSON response to java class with this site Json to java 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比如你的 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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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