Pavel Petrashov
Pavel Petrashov

Reputation: 139

it is impossible to convert JSON into an java object using GSON and RETROFIT

I use Retrofit in my project. And i have problem. My JSON not convert to object.

Here it is my JSON:

{"id":1,"facebookId":"123","uuid":"xU71niPhrqzFqmBFhk5fl4LlvWg6zk42ENG4iMjgl3eJEb9LAcqVcc9NmLHzFIgg","userInfo":{"id":1,"user":null,"email":"[email protected]","firstName":"Pavel","lastName":"Petrashov","name":null,"dob":null,"gender":null,"facebookLink":"link","locale":null,"location":null,"timezone":null,"verified":null,"updatedTime":null},"balancedCustomer":null,"session":{"id":1,"session":"FTKuyeb1BXBzXRZzsAuwPYs4eRIpdi2Z","expirationDate":1424839445000,"ip":null,"user":null}}

Here it is my Retrofit method:

public ApiUser getApiUser(){
        if (null == apiUser) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
            RestAdapter.Builder builder = new RestAdapter.Builder()
                    .setEndpoint(BuildConfig.ROOT_URL_SKIP)
                    .setClient(new OkClient(new OkHttpClient()))
                    .setConverter(new GsonConverter(gson));
            builder.setLogLevel(RestAdapter.LogLevel.FULL);

            RestAdapter restAdapter = builder.build();
            apiUser = restAdapter.create(ApiUser.class);
        }
        return apiUser;
    }

and i get this error:

Error: com.google.gson.JsonSyntaxException: 1424839445000

My classes look like this:

@Table(name = "Users")
public class User extends Model {

    @Expose
    @Column(name = "facebookId")
    @SerializedName("facebookId")
    private String facebookId;

    @Expose
    @Column(name = "uuid")
    @SerializedName("uuid")
    private String uuid;

    @Expose
    @Column(name = "userInfo")
    @SerializedName("userInfo")
    private UserInfo userInfo;

    @Expose
    @Column(name = "balancedCustomer")
    @SerializedName("balancedCustomer")
    private BalancedCustomer balancedCustomer;

    @Expose
    @Column(name = "session")
    @SerializedName("session")
    private Session session;

    public User() {
        super();
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public String getFacebookId() {
        return facebookId;
    }

    public void setFacebookId(String facebookId) {
        this.facebookId = facebookId;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getUuid() {
        return uuid;
    }

    public BalancedCustomer getBalancedCustomer() {
        return balancedCustomer;
    }

    public void setBalancedCustomer(BalancedCustomer balancedCustomer) {
        this.balancedCustomer = balancedCustomer;
    }
}

edited, this whole class.

@Table(name = "Sessions")
public class Session extends Model {

    @Expose
    @Column(name = "session")
    @SerializedName("session")
    private String session;

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private Date expirationDate;

...

Upvotes: 1

Views: 719

Answers (5)

Gowtham Raj
Gowtham Raj

Reputation: 2955

Change the data type of the Session->expiryDate to Long or String then when u want to convert that to a Date object just use this.

Date date=new Date(expirationDate);

Upvotes: 0

Pavel Petrashov
Pavel Petrashov

Reputation: 139

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }
            }).create();

thanks for the link @Deepak Bala enter link description here

Upvotes: 1

dvs
dvs

Reputation: 644

this will solve your problem : solution

Let me know if you face any problem with it.

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Get expirationDate as long instead of Date :

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private long expirationDate;

Upvotes: 0

learner
learner

Reputation: 3110

Don't try to get expirationDate with Date object because in json it is like unformatted.what you should do ,receive like string or double in model classes then inside application you can change or parse to date format.

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private String expirationDate;

Upvotes: 0

Related Questions