D
D
di_gangsta2019-03-09 18:40:14
Java
di_gangsta, 2019-03-09 18:40:14

How to parse a double JsonObject in Retrofit?

Using the Retrofit library, process the response from the server, which looks like this

{"token":"WFRHaUlzM0t1dlc1T1N6dkdpNlZIQlZ1RXV3c0l2dGwyc2NFN0RsU3pTbDdyYWpMNDJua0NqNSs2WmJKTWpJVA","errorNumber":3,"errorDescription":"invalid token","user":{"id":null,"type":0}}

The difficulty is that the "user" field contains elements embedded in itself and I just don't get them. Here is my Callback code in Java. Tell me how to do it right.
import com.google.gson.annotations.SerializedName;
import org.json.JSONObject;

class TattooMarketServerInfo {
    @SerializedName("token")
    private String token;
    @SerializedName("errorNumber")
    private String errorNumber;
    @SerializedName("errorDescription")
    private String errorDescription;
// user fields
    @SerializedName("user")
    private JSONObject user;
    @SerializedName("type")
    private int type;
    @SerializedName("id")
    private String id;


    @Override
    public String toString() {
        return "TattooMarketServerInfo:" + '\n' +
                "token = " + token + '\n' +
                "errorNumber = " + errorNumber + '\n' +
                "errorDescription = " + errorDescription + '\n' +
                "user = " + user.toString()  + '\n' +
                "id = " + id + '\n' +
                "type = " + type + '\n';
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
illuzor, 2019-03-09
@di_gangsta

Create a separate User class with id and type fields.
In the TattooMarketServerInfo class, the user field type must be User, not JSONObject, and the type and id fields from the TattooMarketServerInfo class must be removed.

G
Gregary, 2020-03-02
@Gregary

Here is an excellent tutorial in Russian and Kotlin on how to work with GSON and Retrofit androidschool.ru/courses/android-retrofit-and-gson

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question