简体   繁体   中英

Gson deserialization throws a casting error

I am trying to use the Gson library to deserialize a Json object, everything works out to be fine but this one data type and class is giving me errors

E/AndroidRuntime(11573): java.lang.ClassCastException: java.util.ArrayList cannot be cast to frameWork.DataManager.Token

Sample Json:

[{"ID":-1,"Token":"APA91bFQdK7qVJheMBsulWBYYVAan5AwLyMT5cL_BYaoDXxlDGoxabZ0XRnn6osNtmA1KvDOFb3mHkFDpXJ_zxshFSLY0cbt0dMmSZFDCXkDKfha2M_TRMwyB86c38do5Ngje_AOHfasfasfasfafasfascascrvfbdfbdfbfsfafasfasfasfaok","OS":"Android","Application":"Merchant"}]

Class object

public class Token {

    public int ID;
    public String Token;
    public String OS;
    public String Application;

}

Actual Code where result is the Json String

Gson gson = new Gson();

this.mainScreenActivity.dataStorageManager.deviceToken = gson.fromJson(
        result, new TypeToken<List<Token>>() {
}.getType());

Thoughts?

You are using a

new TypeToken<List<Token>>

You should be assigning the return value of Gson#fromJson to a

List<Token>

not a

Token

which

this.mainScreenActivity.dataStorageManager.deviceToken

seems to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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