使用带有改造 API 的 GSON 解析 JSON 时出错

问题描述 投票:0回答:1

这是我正在使用的 POJO 或模型类,我能够从服务器获取响应,但它抛出 SyntaxException 和 MalformedJSONException //模型类:

public class AppUser {
   public Integer userID;
   public String username;
   public String password;
   public String emailID;
   public String accessToken;

}

//JSON Retrieved:
{
  "userID": "123456",
  "username": "rkumar",
  "password": "rohitk",
  "emailID": "[email protected]",
  "accessToken": "1weErtYo90ds8i9"
}

//JSON 反序列化器

public class AppUserDeserializer implements JsonDeserializer<AppUser> {
    @Override
    public AppUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Log.d("Json element",json.toString());
        return new Gson().fromJson(json, AppUser.class);
    }
}

//Rest 客户端构造函数

public RestClient()
    {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(AppUser.class,  new AppUserDeserializer())
                .create();

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(baseURL)
                .setConverter(new GsonConverter(gson))
                .build();

        signInService = restAdapter.create(SignInService.class);
    }
gson retrofit
1个回答
0
投票

添加

@Serializable
注释:

对于

Kotlin

@Serializable
data class MyDataClass(...)

对于

Java

import java.io.Serializable;
public class MyDataClass implements Serializable {}

有关此解决方案的更多详细信息:[https://stackoverflow.com/a/77796179/14037648]

© www.soinside.com 2019 - 2024. All rights reserved.