micsha123
micsha123

Reputation: 675

Retrofit 2.0 with GsonConverterFactory return null for fields with non-english letters in name

I've some json, for example:

{
    "id": 12,
    "title": "Title",
    "photo": [
        "image.png"
    ],
    "options": {
        "Поле": "150"
    }
}

I create model class for parsing:

public class Model implements Serializable {

public int id;
public String title;
public String[] photo;
@SerializedName("option")
public Options option;

public Model(int id, String title, String[] photo, Options option) {
    this.id = id;
    this.title = title;
    this.photo = photo;
    this.option = option;
}

public class Options implements Serializable {

    @SerializedName("Поле")
    public String pole;

    public Options(String pole) {
        this.pole = pole;
    }
}
}

But unfortunately I have null in my Model.Option.pole.

I think, the problem is in encoding. Am I right? And how can I solve this issue?

P.S.: In example - cyrillic name of field

Upvotes: 1

Views: 1157

Answers (1)

Fred
Fred

Reputation: 17095

I think the problem is @SerializedName("option") should be @SerializedName("options") according to your json (note the plural form - options)

The annotation SerializedName tells gson what it should use to serialize and deserialize the attribute key in the json. You have options in your json but you told gson to serialize and deserialize option.

Upvotes: 1

Related Questions